Skip to content

Commit 773e110

Browse files
committed
Actually update profile after setting changing the name
1 parent ad6da95 commit 773e110

File tree

6 files changed

+57
-1
lines changed

6 files changed

+57
-1
lines changed

examples/profile-update-name-async.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
))->withAccessToken(
2323
$config['access_token']['token'],
2424
$config['access_token']['secret']
25-
)->profile()->done(function (ProfileInterface $profile) use ($argv) {
25+
)->profile()->then(function (ProfileInterface $profile) use ($argv) {
26+
echo 'Fetched profile', PHP_EOL;
2627
resource_pretty_print($profile);
2728
$emojis = [
2829
'😈 ',
@@ -72,11 +73,18 @@
7273
'🎵',
7374
'🎶',
7475
];
76+
echo 'Setting new name', PHP_EOL;
7577
$profile = $profile->withName(sprintf(
7678
$argv[1],
7779
$emojis[random_int(0, count($emojis) - 1)]
7880
));
7981
resource_pretty_print($profile);
82+
83+
echo 'Updating profile', PHP_EOL;
84+
return $profile->putProfile();
85+
})->done(function (ProfileInterface $profile) {
86+
echo 'Profile updated', PHP_EOL;
87+
resource_pretty_print($profile);
8088
});
8189

8290
$loop->run();

src/Resource/Async/Profile.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
namespace ApiClients\Client\Twitter\Resource\Async;
44

55
use ApiClients\Client\Twitter\Resource\Profile as BaseProfile;
6+
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand;
7+
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand;
8+
use Psr\Http\Message\ResponseInterface;
9+
use React\Promise\PromiseInterface;
10+
use RingCentral\Psr7\Request;
11+
use function React\Promise\resolve;
612

713
class Profile extends BaseProfile
814
{
15+
public function putProfile(): PromiseInterface
16+
{
17+
$fields = [];
18+
foreach ($this->changedFields as $field) {
19+
$fields[$field] = $this->$field;
20+
}
21+
22+
$uri = 'account/update_profile.json?' . http_build_query($fields);
23+
24+
return $this->handleCommand(new RequestCommand(
25+
new Request('POST', $uri)
26+
))->then(function (ResponseInterface $response) {
27+
return resolve($this->handleCommand(new HydrateCommand('Profile', $response->getBody()->getJson())));
28+
});
29+
}
30+
931
public function refresh() : Profile
1032
{
1133
throw new \Exception('TODO: create refresh method!');

src/Resource/EmptyProfile.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,9 @@ public function withName(string $name): ProfileInterface
7171
{
7272
return clone $this;
7373
}
74+
75+
public function putProfile()
76+
{
77+
78+
}
7479
}

src/Resource/Profile.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ abstract class Profile extends AbstractResource implements ProfileInterface
4545
*/
4646
protected $description;
4747

48+
/**
49+
* @var array
50+
*/
51+
protected $changedFields = [];
52+
4853
/**
4954
* @return int
5055
*/
@@ -109,6 +114,12 @@ public function withName(string $name): ProfileInterface
109114
{
110115
$clone = clone $this;
111116
$clone->name = $name;
117+
$clone->changedFields['name'] = 'name';
112118
return $clone;
113119
}
120+
121+
public function putProfile()
122+
{
123+
124+
}
114125
}

src/Resource/ProfileInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@ public function description() : string;
4848
* @return static
4949
*/
5050
public function withName(string $name);
51+
52+
/**
53+
* @return static
54+
*/
55+
public function putProfile();
5156
}

src/Resource/Sync/Profile.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
class Profile extends BaseProfile
1010
{
11+
public function putProfile()
12+
{
13+
// TODO: Implement putProfile() method.
14+
}
15+
1116
public function refresh() : Profile
1217
{
1318
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ProfileInterface $profile) {

0 commit comments

Comments
 (0)