1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Twitter; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Client; |
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Factory; |
7
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\HydrateCommand; |
8
|
|
|
use ApiClients\Foundation\Options; |
9
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
10
|
|
|
use ApiClients\Foundation\Transport\Options as TransportOptions; |
11
|
|
|
use ApiClients\Middleware\Oauth1\Oauth1Middleware; |
12
|
|
|
use ApiClients\Middleware\Oauth1\Options as Oauth1Options; |
13
|
|
|
use ApiClients\Tools\CommandBus\CommandBusInterface; |
14
|
|
|
use ApiClients\Tools\Psr7\Oauth1\Definition; |
15
|
|
|
use GuzzleHttp\Psr7\Request; |
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
17
|
|
|
use React\EventLoop\LoopInterface; |
18
|
|
|
use React\Promise\PromiseInterface; |
19
|
|
|
use function React\Promise\resolve; |
20
|
|
|
|
21
|
|
|
final class AsyncClient implements AsyncClientInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Client |
26
|
|
|
*/ |
27
|
|
|
protected $client; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AsyncStreamingClient |
36
|
|
|
*/ |
37
|
|
|
protected $streamingClient; |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $consumerKey; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $consumerSecret; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var LoopInterface |
50
|
|
|
*/ |
51
|
|
|
private $loop; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* AsyncClient constructor. |
55
|
|
|
* @param string $consumerKey |
56
|
|
|
* @param string $consumerSecret |
57
|
|
|
* @param LoopInterface $loop |
58
|
|
|
* @param array $options |
59
|
|
|
* @param Client|null $client |
60
|
|
|
*/ |
61
|
2 |
|
public function __construct( |
62
|
|
|
string $consumerKey, |
63
|
|
|
string $consumerSecret, |
64
|
|
|
LoopInterface $loop, |
65
|
|
|
array $options = [], |
66
|
|
|
Client $client = null |
67
|
|
|
) { |
68
|
2 |
|
$this->consumerKey = $consumerKey; |
69
|
2 |
|
$this->consumerSecret = $consumerSecret; |
70
|
2 |
|
$this->loop = $loop; |
71
|
|
|
|
72
|
2 |
|
if (!($client instanceof Client)) { |
73
|
2 |
|
$this->options = ApiSettings::getOptions( |
74
|
2 |
|
$consumerKey, |
75
|
2 |
|
$consumerSecret, |
76
|
2 |
|
'Async', |
77
|
2 |
|
$options |
78
|
|
|
); |
79
|
|
|
|
80
|
2 |
|
$client = Factory::create($this->loop, $this->options); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$this->client = $client; |
84
|
2 |
|
} |
85
|
|
|
|
86
|
2 |
|
public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient |
87
|
|
|
{ |
88
|
2 |
|
$options = $this->options; |
89
|
|
|
// @codingStandardsIgnoreStart |
90
|
2 |
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken); |
91
|
2 |
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret); |
92
|
|
|
// @codingStandardsIgnoreEnd |
93
|
|
|
|
94
|
2 |
|
return new self( |
95
|
2 |
|
$this->consumerKey, |
96
|
2 |
|
$this->consumerSecret, |
97
|
2 |
|
$this->loop, |
98
|
2 |
|
$options |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function withOutAccessToken(): AsyncClient |
103
|
|
|
{ |
104
|
1 |
|
$options = $this->options; |
105
|
|
|
// @codingStandardsIgnoreStart |
106
|
1 |
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
|
|
|
|
107
|
1 |
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
108
|
|
|
} |
109
|
1 |
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
|
|
|
|
110
|
1 |
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
111
|
|
|
} |
112
|
|
|
// @codingStandardsIgnoreEnd |
113
|
|
|
|
114
|
1 |
|
return new self( |
115
|
1 |
|
$this->consumerKey, |
116
|
1 |
|
$this->consumerSecret, |
117
|
1 |
|
$this->loop, |
118
|
1 |
|
$options |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getCommandBus(): CommandBusInterface |
123
|
|
|
{ |
124
|
|
|
return $this->client->getFromContainer(CommandBusInterface::class); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function profile(): PromiseInterface |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
return $this->client->handle(new RequestCommand( |
130
|
|
|
new Request('GET', 'account/verify_credentials.json') |
131
|
|
|
))->then(function (ResponseInterface $response) { |
132
|
|
|
return resolve($this->client->handle(new HydrateCommand('Profile', $response->getBody()->getParsedContents()))); |
|
|
|
|
133
|
|
|
}); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
View Code Duplication |
public function user(string $user): PromiseInterface |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
return $this->client->handle(new RequestCommand( |
139
|
|
|
new Request('GET', 'users/show.json?screen_name=' . $user) |
140
|
|
|
))->then(function (ResponseInterface $response) { |
141
|
|
|
return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getParsedContents()))); |
|
|
|
|
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function tweet(string $status, array $tweet = []): PromiseInterface |
146
|
|
|
{ |
147
|
|
|
$tweet['status'] = $status; |
148
|
|
|
|
149
|
|
|
return $this->client->handle(new RequestCommand( |
150
|
|
|
new Request('POST', 'statuses/update.json?' . http_build_query($tweet)) |
151
|
|
|
))->then(function (ResponseInterface $response) { |
152
|
|
|
return resolve($this->client->handle(new HydrateCommand('Tweet', $response->getBody()->getParsedContents()))); |
|
|
|
|
153
|
|
|
}); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function stream(): AsyncStreamingClientInterface |
157
|
|
|
{ |
158
|
|
|
if (!($this->streamingClient instanceof AsyncStreamingClient)) { |
159
|
|
|
$this->streamingClient = new AsyncStreamingClient($this->client); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->streamingClient; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: