|
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\Oauth1\Middleware\Oauth1Middleware; |
|
9
|
|
|
use ApiClients\Foundation\Oauth1\Options as Oauth1Options; |
|
10
|
|
|
use ApiClients\Foundation\Options; |
|
11
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\RequestCommand; |
|
12
|
|
|
use ApiClients\Foundation\Transport\CommandBus\Command\StreamingRequestCommand; |
|
13
|
|
|
use ApiClients\Foundation\Transport\Options as TransportOptions; |
|
14
|
|
|
use ApiClients\Tools\Psr7\Oauth1\Definition; |
|
15
|
|
|
use GuzzleHttp\Psr7\Request; |
|
16
|
|
|
use Psr\Http\Message\RequestInterface; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use React\EventLoop\LoopInterface; |
|
19
|
|
|
use React\Promise\PromiseInterface; |
|
20
|
|
|
use Rx\Extra\Operator\CutOperator; |
|
21
|
|
|
use Rx\Observable; |
|
22
|
|
|
use Rx\React\Promise; |
|
23
|
|
|
use function React\Promise\resolve; |
|
24
|
|
|
|
|
25
|
|
|
final class AsyncClient |
|
26
|
|
|
{ |
|
27
|
|
|
const STREAM_DELIMITER = "\r\n"; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $consumerKey; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $consumerSecret; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var LoopInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $loop; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Client |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $client; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
string $consumerKey, |
|
51
|
|
|
string $consumerSecret, |
|
52
|
|
|
LoopInterface $loop, |
|
53
|
|
|
array $options = [], |
|
54
|
|
|
Client $client = null |
|
55
|
|
|
) { |
|
56
|
|
|
$this->consumerKey = $consumerKey; |
|
57
|
|
|
$this->consumerSecret = $consumerSecret; |
|
58
|
|
|
$this->loop = $loop; |
|
59
|
|
|
|
|
60
|
|
|
if (!($client instanceof Client)) { |
|
61
|
|
|
$this->options = ApiSettings::getOptions( |
|
|
|
|
|
|
62
|
|
|
$consumerKey, |
|
63
|
|
|
$consumerSecret, |
|
64
|
|
|
'Async', |
|
65
|
|
|
$options |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$client = Factory::create($this->loop, $this->options); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->client = $client; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient |
|
75
|
|
|
{ |
|
76
|
|
|
$options = $this->options; |
|
77
|
|
|
// @codingStandardsIgnoreStart |
|
78
|
|
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken); |
|
79
|
|
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret); |
|
80
|
|
|
// @codingStandardsIgnoreEnd |
|
81
|
|
|
|
|
82
|
|
|
return new self( |
|
83
|
|
|
$this->consumerKey, |
|
84
|
|
|
$this->consumerSecret, |
|
85
|
|
|
$this->loop, |
|
86
|
|
|
$options |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function withOutAccessToken(): AsyncClient |
|
91
|
|
|
{ |
|
92
|
|
|
$options = $this->options; |
|
93
|
|
|
// @codingStandardsIgnoreStart |
|
94
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
|
|
|
|
|
|
95
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
|
96
|
|
|
} |
|
97
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
|
|
|
|
|
|
98
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
|
99
|
|
|
} |
|
100
|
|
|
// @codingStandardsIgnoreEnd |
|
101
|
|
|
|
|
102
|
|
|
return new self( |
|
103
|
|
|
$this->consumerKey, |
|
104
|
|
|
$this->consumerSecret, |
|
105
|
|
|
$this->loop, |
|
106
|
|
|
$options |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function user(string $user): PromiseInterface |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->client->handle(new RequestCommand( |
|
113
|
|
|
new Request('GET', 'users/show.json?screen_name=' . $user) |
|
114
|
|
|
))->then(function (ResponseInterface $response) { |
|
115
|
|
|
return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson()))); |
|
|
|
|
|
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function sampleStream(): Observable |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->stream( |
|
122
|
|
|
new Request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json') |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function filteredStream(array $filter = []): Observable |
|
127
|
|
|
{ |
|
128
|
|
|
$postData = http_build_query($filter); |
|
129
|
|
|
return $this->stream( |
|
130
|
|
|
new Request( |
|
131
|
|
|
'POST', |
|
132
|
|
|
'https://stream.twitter.com/1.1/statuses/filter.json', |
|
133
|
|
|
[ |
|
134
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
135
|
|
|
'Content-Length' => strlen($postData), |
|
136
|
|
|
], |
|
137
|
|
|
$postData |
|
138
|
|
|
) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function stream(RequestInterface $request): Observable |
|
143
|
|
|
{ |
|
144
|
|
|
return Promise::toObservable($this->client->handle(new StreamingRequestCommand( |
|
145
|
|
|
$request |
|
146
|
|
|
)))->switchLatest()->lift(function () { |
|
147
|
|
|
return new CutOperator(self::STREAM_DELIMITER); |
|
148
|
|
|
})->filter(function (string $json) { |
|
149
|
|
|
return trim($json) !== ''; // To keep the stream alive Twitter sends an empty line at times |
|
150
|
|
|
})->jsonDecode()->flatMap(function (array $document) { |
|
151
|
|
|
if (isset($document['delete'])) { |
|
152
|
|
|
return Promise::toObservable($this->client->handle( |
|
153
|
|
|
new HydrateCommand('DeletedTweet', $document['delete']) |
|
154
|
|
|
)); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return Promise::toObservable($this->client->handle(new HydrateCommand('Tweet', $document))); |
|
158
|
|
|
}); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: