Skip to content

Commit 855cbe6

Browse files
committed
Update profile for sync client
1 parent 642331c commit 855cbe6

File tree

5 files changed

+181
-62
lines changed

5 files changed

+181
-62
lines changed

examples/emoji.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
return [
4+
'😈 ',
5+
'👾 ',
6+
'🤖 ',
7+
'🦄 ',
8+
'🐯 ',
9+
'🦁 ',
10+
'🐆 ',
11+
'🐅 ',
12+
'🐃 ',
13+
'🐦 ',
14+
'🦎 ',
15+
'🐲 ',
16+
'🐉 ',
17+
'🦋 ',
18+
'🐞 ',
19+
'🕷 ',
20+
'🕸 ',
21+
'🍀 ',
22+
'🍔 ',
23+
'🥞 ',
24+
'🌭 ',
25+
'🍕 ',
26+
'🍺 ',
27+
'🍻 ',
28+
'🥃',
29+
'🌍',
30+
'🌎',
31+
'🌏',
32+
'🌐',
33+
'🗺',
34+
'🏔',
35+
'',
36+
'🌋',
37+
'',
38+
'🌪',
39+
'🌀',
40+
'🌈',
41+
'',
42+
'',
43+
'',
44+
'🔥',
45+
'🎃',
46+
'🎮',
47+
'🔊',
48+
'🎵',
49+
'🎶',
50+
];

examples/profile-update-name-async.php

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
use React\EventLoop\Factory;
66
use function ApiClients\Foundation\resource_pretty_print;
77

8-
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
9-
$config = require 'resolve_config.php';
10-
118
if (!isset($argv[1])) {
129
echo 'This example requires you to pass a username for example \'php profile-update-name-async.php "Cees-Jan %s Kiewiet"\'.', PHP_EOL;
1310
echo 'The %s in there will be replaced with a random emoji.', PHP_EOL;
1411
exit(255);
1512
}
1613

14+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
15+
$config = require 'resolve_config.php';
16+
$emojis = require 'emoji.php';
17+
1718
$loop = Factory::create();
1819
$client = (new AsyncClient(
1920
$config['consumer']['key'],
@@ -22,57 +23,9 @@
2223
))->withAccessToken(
2324
$config['access_token']['token'],
2425
$config['access_token']['secret']
25-
)->profile()->then(function (ProfileInterface $profile) use ($argv) {
26+
)->profile()->then(function (ProfileInterface $profile) use ($argv, $emojis) {
2627
echo 'Fetched profile', PHP_EOL;
2728
resource_pretty_print($profile);
28-
$emojis = [
29-
'😈 ',
30-
'👾 ',
31-
'🤖 ',
32-
'🦄 ',
33-
'🐯 ',
34-
'🦁 ',
35-
'🐆 ',
36-
'🐅 ',
37-
'🐃 ',
38-
'🐦 ',
39-
'🦎 ',
40-
'🐲 ',
41-
'🐉 ',
42-
'🦋 ',
43-
'🐞 ',
44-
'🕷 ',
45-
'🕸 ',
46-
'🍀 ',
47-
'🍔 ',
48-
'🥞 ',
49-
'🌭 ',
50-
'🍕 ',
51-
'🍺 ',
52-
'🍻 ',
53-
'🥃',
54-
'🌍',
55-
'🌎',
56-
'🌏',
57-
'🌐',
58-
'🗺',
59-
'🏔',
60-
'',
61-
'🌋',
62-
'',
63-
'🌪',
64-
'🌀',
65-
'🌈',
66-
'',
67-
'',
68-
'',
69-
'🔥',
70-
'🎃',
71-
'🎮',
72-
'🔊',
73-
'🎵',
74-
'🎶',
75-
];
7629
echo 'Setting new name', PHP_EOL;
7730
$profile = $profile->withName(sprintf(
7831
$argv[1],

examples/profile-update-name.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use ApiClients\Client\Twitter\Client;
4+
use function ApiClients\Foundation\resource_pretty_print;
5+
6+
if (!isset($argv[1])) {
7+
echo 'This example requires you to pass a username for example \'php profile-update-name-async.php "Cees-Jan %s Kiewiet"\'.', PHP_EOL;
8+
echo 'The %s in there will be replaced with a random emoji.', PHP_EOL;
9+
exit(255);
10+
}
11+
12+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';
13+
$config = require 'resolve_config.php';
14+
$emojis = require 'emoji.php';
15+
16+
$profile = (new Client(
17+
$config['consumer']['key'],
18+
$config['consumer']['secret']
19+
))->withAccessToken(
20+
$config['access_token']['token'],
21+
$config['access_token']['secret']
22+
)->profile();
23+
24+
echo 'Fetched profile', PHP_EOL;
25+
resource_pretty_print($profile);
26+
echo 'Setting new name', PHP_EOL;
27+
$profile = $profile->withName(sprintf(
28+
$argv[1],
29+
$emojis[random_int(0, count($emojis) - 1)]
30+
));
31+
resource_pretty_print($profile);
32+
33+
echo 'Updating profile', PHP_EOL;
34+
$profile = $profile->putProfile();
35+
echo 'Profile updated', PHP_EOL;
36+
resource_pretty_print($profile);

src/Client.php

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@
22

33
namespace ApiClients\Client\Twitter;
44

5+
use ApiClients\Client\Twitter\Resource\Async\Profile;
56
use ApiClients\Client\Twitter\Resource\ProfileInterface;
67
use ApiClients\Client\Twitter\Resource\TweetInterface;
78
use ApiClients\Client\Twitter\Resource\UserInterface;
9+
use ApiClients\Foundation\Client as FoundationClient;
10+
use ApiClients\Foundation\Factory;
11+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand;
12+
use ApiClients\Foundation\Oauth1\Middleware\Oauth1Middleware;
13+
use ApiClients\Foundation\Oauth1\Options as Oauth1Options;
14+
use ApiClients\Foundation\Options;
15+
use ApiClients\Foundation\Transport\Options as TransportOptions;
16+
use ApiClients\Tools\Psr7\Oauth1\Definition;
817
use React\EventLoop\Factory as LoopFactory;
918
use React\EventLoop\LoopInterface;
1019
use function Clue\React\Block\await;
1120

1221
final class Client implements ClientInterface
1322
{
23+
/**
24+
* @var string
25+
*/
26+
private $consumerKey;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $consumerSecret;
32+
1433
/**
1534
* @var LoopInterface
1635
*/
@@ -19,32 +38,83 @@ final class Client implements ClientInterface
1938
/**
2039
* @var AsyncClient
2140
*/
41+
protected $asyncClient;
42+
43+
/**
44+
* @var FoundationClient
45+
*/
2246
protected $client;
2347

2448
/**
2549
* @var StreamingClient
2650
*/
2751
protected $streamingClient;
2852

53+
/**
54+
* @var array
55+
*/
56+
protected $options;
57+
2958
public function __construct(
3059
string $consumerKey,
3160
string $consumerSecret
3261
) {
62+
$this->consumerKey = $consumerKey;
63+
$this->consumerSecret = $consumerSecret;
3364
$this->loop = LoopFactory::create();
34-
$this->client = new AsyncClient($consumerKey, $consumerSecret, $this->loop);
65+
66+
$this->options = ApiSettings::getOptions(
67+
$consumerKey,
68+
$consumerSecret,
69+
'Sync'
70+
);
71+
72+
$this->client = Factory::create($this->loop, $this->options);
73+
74+
$this->asyncClient = new AsyncClient($consumerKey, $consumerSecret, $this->loop, [], $this->client);
3575
}
3676

3777
public function withAccessToken(string $accessToken, string $accessTokenSecret): Client
3878
{
79+
$options = $this->options;
80+
// @codingStandardsIgnoreStart
81+
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken);
82+
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret);
83+
// @codingStandardsIgnoreEnd
84+
3985
$clone = clone $this;
40-
$clone->client = $this->client->withAccessToken($accessToken, $accessTokenSecret);
86+
$clone->client = Factory::create($this->loop, $options);
87+
$clone->asyncClient = (new AsyncClient(
88+
$this->consumerKey,
89+
$this->consumerSecret,
90+
$this->loop,
91+
[],
92+
$this->client
93+
))->withAccessToken($accessToken, $accessTokenSecret);
4194
return $clone;
4295
}
4396

4497
public function withOutAccessToken(): Client
4598
{
99+
$options = $this->options;
100+
// @codingStandardsIgnoreStart
101+
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) {
102+
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]);
103+
}
104+
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) {
105+
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]);
106+
}
107+
// @codingStandardsIgnoreEnd
108+
46109
$clone = clone $this;
47-
$clone->client = $this->client->withOutAccessToken();
110+
$clone->client = Factory::create($this->loop, $options);
111+
$clone->asyncClient = (new AsyncClient(
112+
$this->consumerKey,
113+
$this->consumerSecret,
114+
$this->loop,
115+
[],
116+
$this->client
117+
))->withOutAccessToken();
48118
return $clone;
49119
}
50120

@@ -53,8 +123,8 @@ public function stream(): StreamingClient
53123
if (!($this->streamingClient instanceof StreamingClient)) {
54124
$this->streamingClient = new StreamingClient(
55125
$this->loop,
56-
$this->client->getCommandBus(),
57-
$this->client->stream()
126+
$this->asyncClient->getCommandBus(),
127+
$this->asyncClient->stream()
58128
);
59129
}
60130

@@ -64,23 +134,25 @@ public function stream(): StreamingClient
64134
public function tweet(string $tweet): TweetInterface
65135
{
66136
return await(
67-
$this->client->tweet($tweet),
137+
$this->asyncClient->tweet($tweet),
68138
$this->loop
69139
);
70140
}
71141

72142
public function profile(): ProfileInterface
73143
{
74144
return await(
75-
$this->client->profile(),
145+
$this->asyncClient->profile()->then(function (Profile $profile) {
146+
return $this->client->handle(new BuildSyncFromAsyncCommand(ProfileInterface::HYDRATE_CLASS, $profile));
147+
}),
76148
$this->loop
77149
);
78150
}
79151

80152
public function user(string $tweet): UserInterface
81153
{
82154
return await(
83-
$this->client->user($tweet),
155+
$this->asyncClient->user($tweet),
84156
$this->loop
85157
);
86158
}

src/Resource/Sync/Profile.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
namespace ApiClients\Client\Twitter\Resource\Sync;
44

5+
use ApiClients\Client\Twitter\Resource\Async\Profile as AsyncProfile;
56
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
67
use ApiClients\Client\Twitter\Resource\Profile as BaseProfile;
78
use ApiClients\Client\Twitter\Resource\ProfileInterface;
9+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildSyncFromAsyncCommand;
810

911
class Profile extends BaseProfile
1012
{
11-
public function putProfile()
13+
public function putProfile(): Profile
1214
{
13-
// TODO: Implement putProfile() method.
15+
return $this->wait(
16+
$this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (AsyncProfile $profile) {
17+
return $profile->putProfile();
18+
})->then(function (Profile $profile) {
19+
return $this->handleCommand(new BuildSyncFromAsyncCommand(self::HYDRATE_CLASS, $profile));
20+
})
21+
);
1422
}
1523

1624
public function refresh() : Profile

0 commit comments

Comments
 (0)