1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Client\Twitter; |
4
|
|
|
|
5
|
|
|
use ApiClients\Client\Twitter\Resource\Async\Profile; |
6
|
|
|
use ApiClients\Client\Twitter\Resource\ProfileInterface; |
7
|
|
|
use ApiClients\Client\Twitter\Resource\TweetInterface; |
8
|
|
|
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\Options; |
13
|
|
|
use ApiClients\Foundation\Transport\Options as TransportOptions; |
14
|
|
|
use ApiClients\Middleware\Oauth1\Oauth1Middleware; |
15
|
|
|
use ApiClients\Middleware\Oauth1\Options as Oauth1Options; |
16
|
|
|
use ApiClients\Tools\Psr7\Oauth1\Definition; |
17
|
|
|
use React\EventLoop\Factory as LoopFactory; |
18
|
|
|
use React\EventLoop\LoopInterface; |
19
|
|
|
use function Clue\React\Block\await; |
20
|
|
|
|
21
|
|
|
final class Client implements ClientInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoopInterface |
26
|
|
|
*/ |
27
|
|
|
protected $loop; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var AsyncClient |
31
|
|
|
*/ |
32
|
|
|
protected $asyncClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var FoundationClient |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var StreamingClient |
41
|
|
|
*/ |
42
|
|
|
protected $streamingClient; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $options; |
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $consumerKey; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $consumerSecret; |
57
|
|
|
|
58
|
1 |
|
public function __construct( |
59
|
|
|
string $consumerKey, |
60
|
|
|
string $consumerSecret |
61
|
|
|
) { |
62
|
1 |
|
$this->consumerKey = $consumerKey; |
63
|
1 |
|
$this->consumerSecret = $consumerSecret; |
64
|
1 |
|
$this->loop = LoopFactory::create(); |
65
|
|
|
|
66
|
1 |
|
$this->options = ApiSettings::getOptions( |
67
|
|
|
$consumerKey, |
68
|
|
|
$consumerSecret, |
69
|
1 |
|
'Sync' |
70
|
|
|
); |
71
|
|
|
|
72
|
1 |
|
$this->client = Factory::create($this->loop, $this->options); |
73
|
|
|
|
74
|
1 |
|
$this->asyncClient = new AsyncClient($consumerKey, $consumerSecret, $this->loop, [], $this->client); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
public function withAccessToken(string $accessToken, string $accessTokenSecret): Client |
78
|
|
|
{ |
79
|
1 |
|
$options = $this->options; |
80
|
|
|
// @codingStandardsIgnoreStart |
81
|
1 |
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN] = new Definition\AccessToken($accessToken); |
82
|
1 |
|
$options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET] = new Definition\TokenSecret($accessTokenSecret); |
83
|
|
|
// @codingStandardsIgnoreEnd |
84
|
|
|
|
85
|
1 |
|
$clone = clone $this; |
86
|
1 |
|
$clone->client = Factory::create($this->loop, $options); |
87
|
1 |
|
$clone->asyncClient = (new AsyncClient( |
88
|
1 |
|
$this->consumerKey, |
89
|
1 |
|
$this->consumerSecret, |
90
|
1 |
|
$this->loop, |
91
|
1 |
|
[], |
92
|
1 |
|
$this->client |
93
|
1 |
|
))->withAccessToken($accessToken, $accessTokenSecret); |
94
|
1 |
|
|
95
|
|
|
return $clone; |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
public function withOutAccessToken(): Client |
99
|
1 |
|
{ |
100
|
|
|
$options = $this->options; |
101
|
1 |
|
// @codingStandardsIgnoreStart |
102
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN])) { |
|
|
|
|
103
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::ACCESS_TOKEN]); |
104
|
1 |
|
} |
105
|
|
View Code Duplication |
if (isset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET])) { |
|
|
|
|
106
|
|
|
unset($options[Options::TRANSPORT_OPTIONS][TransportOptions::DEFAULT_REQUEST_OPTIONS][Oauth1Middleware::class][Oauth1Options::TOKEN_SECRET]); |
107
|
|
|
} |
108
|
|
|
// @codingStandardsIgnoreEnd |
109
|
1 |
|
|
110
|
1 |
|
$clone = clone $this; |
111
|
1 |
|
$clone->client = Factory::create($this->loop, $options); |
112
|
1 |
|
$clone->asyncClient = (new AsyncClient( |
113
|
1 |
|
$this->consumerKey, |
114
|
1 |
|
$this->consumerSecret, |
115
|
1 |
|
$this->loop, |
116
|
1 |
|
[], |
117
|
|
|
$this->client |
118
|
1 |
|
)); |
119
|
|
|
|
120
|
|
|
return $clone; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function stream(): StreamingClient |
124
|
|
|
{ |
125
|
|
|
if (!($this->streamingClient instanceof StreamingClient)) { |
126
|
|
|
$this->streamingClient = new StreamingClient( |
127
|
|
|
$this->loop, |
128
|
|
|
$this->asyncClient->getCommandBus(), |
129
|
|
|
$this->asyncClient->stream() |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->streamingClient; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function tweet(string $tweet): TweetInterface |
137
|
|
|
{ |
138
|
|
|
return await( |
139
|
|
|
$this->asyncClient->tweet($tweet), |
|
|
|
|
140
|
|
|
$this->loop |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function profile(): ProfileInterface |
145
|
|
|
{ |
146
|
|
|
return await( |
147
|
|
|
$this->asyncClient->profile()->then(function (Profile $profile) { |
148
|
|
|
return $this->client->handle(new BuildSyncFromAsyncCommand(ProfileInterface::HYDRATE_CLASS, $profile)); |
149
|
|
|
}), |
150
|
|
|
$this->loop |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function user(string $tweet): UserInterface |
155
|
|
|
{ |
156
|
|
|
return await( |
157
|
|
|
$this->asyncClient->user($tweet), |
158
|
|
|
$this->loop |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.