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\CommandBus\CommandBus; |
15
|
|
|
use ApiClients\Tools\Psr7\Oauth1\Definition; |
16
|
|
|
use GuzzleHttp\Psr7\Request; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use React\EventLoop\LoopInterface; |
20
|
|
|
use React\Promise\PromiseInterface; |
21
|
|
|
use Rx\Extra\Operator\CutOperator; |
22
|
|
|
use Rx\Observable; |
23
|
|
|
use Rx\React\Promise; |
24
|
|
|
use function React\Promise\resolve; |
25
|
|
|
|
26
|
|
|
final class AsyncClient |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $consumerKey; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $consumerSecret; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var LoopInterface |
40
|
|
|
*/ |
41
|
|
|
private $loop; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Client |
45
|
|
|
*/ |
46
|
|
|
protected $client; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var AsyncStreamingClient |
50
|
|
|
*/ |
51
|
|
|
protected $streamingClient; |
52
|
|
|
|
53
|
|
|
public function __construct( |
54
|
|
|
string $consumerKey, |
55
|
|
|
string $consumerSecret, |
56
|
|
|
LoopInterface $loop, |
57
|
|
|
array $options = [], |
58
|
|
|
Client $client = null |
59
|
|
|
) { |
60
|
|
|
$this->consumerKey = $consumerKey; |
61
|
|
|
$this->consumerSecret = $consumerSecret; |
62
|
|
|
$this->loop = $loop; |
63
|
|
|
|
64
|
|
|
if (!($client instanceof Client)) { |
65
|
|
|
$this->options = ApiSettings::getOptions( |
|
|
|
|
66
|
|
|
$consumerKey, |
67
|
|
|
$consumerSecret, |
68
|
|
|
'Async', |
69
|
|
|
$options |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$client = Factory::create($this->loop, $this->options); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->client = $client; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function withAccessToken(string $accessToken, string $accessTokenSecret): AsyncClient |
79
|
|
|
{ |
80
|
|
|
$options = $this->options; |
81
|
|
|
// @codingStandardsIgnoreStart |
82
|
|
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken); |
83
|
|
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret); |
84
|
|
|
// @codingStandardsIgnoreEnd |
85
|
|
|
|
86
|
|
|
return new self( |
87
|
|
|
$this->consumerKey, |
88
|
|
|
$this->consumerSecret, |
89
|
|
|
$this->loop, |
90
|
|
|
$options |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function withOutAccessToken(): AsyncClient |
95
|
|
|
{ |
96
|
|
|
$options = $this->options; |
97
|
|
|
// @codingStandardsIgnoreStart |
98
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
|
|
|
|
99
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
100
|
|
|
} |
101
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
|
|
|
|
102
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
103
|
|
|
} |
104
|
|
|
// @codingStandardsIgnoreEnd |
105
|
|
|
|
106
|
|
|
return new self( |
107
|
|
|
$this->consumerKey, |
108
|
|
|
$this->consumerSecret, |
109
|
|
|
$this->loop, |
110
|
|
|
$options |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getCommandBus(): CommandBus |
115
|
|
|
{ |
116
|
|
|
return $this->client->getFromContainer(CommandBus::class); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function user(string $user): PromiseInterface |
120
|
|
|
{ |
121
|
|
|
return $this->client->handle(new RequestCommand( |
122
|
|
|
new Request('GET', 'users/show.json?screen_name=' . $user) |
123
|
|
|
))->then(function (ResponseInterface $response) { |
124
|
|
|
return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson()))); |
|
|
|
|
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function stream(): AsyncStreamingClient |
129
|
|
|
{ |
130
|
|
|
if (!($this->streamingClient instanceof AsyncStreamingClient)) { |
131
|
|
|
$this->streamingClient = new AsyncStreamingClient($this->client); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->streamingClient; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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: