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