These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\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 Rx\Observable; |
||
20 | use function React\Promise\resolve; |
||
21 | |||
22 | final class AsyncClient implements AsyncClientInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $consumerKey; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $consumerSecret; |
||
33 | |||
34 | /** |
||
35 | * @var LoopInterface |
||
36 | */ |
||
37 | private $loop; |
||
38 | |||
39 | /** |
||
40 | * @var Client |
||
41 | */ |
||
42 | protected $client; |
||
43 | |||
44 | /** |
||
45 | * @var AsyncStreamingClient |
||
46 | */ |
||
47 | protected $streamingClient; |
||
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( |
||
0 ignored issues
–
show
|
|||
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 getCommandBus(): CommandBusInterface |
||
111 | { |
||
112 | return $this->client->getFromContainer(CommandBusInterface::class); |
||
113 | } |
||
114 | |||
115 | public function user(string $user): PromiseInterface |
||
116 | { |
||
117 | return $this->client->handle(new RequestCommand( |
||
118 | new Request('GET', 'users/show.json?screen_name=' . $user) |
||
119 | ))->then(function (ResponseInterface $response) { |
||
120 | return resolve($this->client->handle(new HydrateCommand('User', $response->getBody()->getJson()))); |
||
121 | }); |
||
122 | } |
||
123 | |||
124 | public function stream(): AsyncStreamingClient |
||
125 | { |
||
126 | if (!($this->streamingClient instanceof AsyncStreamingClient)) { |
||
127 | $this->streamingClient = new AsyncStreamingClient($this->client); |
||
128 | } |
||
129 | |||
130 | return $this->streamingClient; |
||
131 | } |
||
132 | } |
||
133 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: