1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterApi package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Gitter; |
11
|
|
|
|
12
|
|
|
use Gitter\Adapters\AdapterInterface; |
13
|
|
|
use Gitter\Adapters\HttpAdapter; |
14
|
|
|
use Gitter\Adapters\StreamAdapter; |
15
|
|
|
use Gitter\Adapters\StreamAdapterInterface; |
16
|
|
|
use Gitter\Adapters\SyncAdapterInterface; |
17
|
|
|
use Gitter\Resources\Groups; |
18
|
|
|
use Gitter\Resources\Messages; |
19
|
|
|
use Gitter\Resources\Rooms; |
20
|
|
|
use Gitter\Resources\Users; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Psr\Log\NullLogger; |
23
|
|
|
use React\EventLoop\Factory; |
24
|
|
|
use React\EventLoop\LoopInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class Client |
28
|
|
|
* @package Gitter |
29
|
|
|
* |
30
|
|
|
* @property-read Rooms $rooms |
31
|
|
|
* @property-read Users $users |
32
|
|
|
* @property-read Messages $messages |
33
|
|
|
* @property-read Groups $groups |
34
|
|
|
* |
35
|
|
|
* @property string $token |
36
|
|
|
* @property LoggerInterface|null $logger |
37
|
|
|
* @property LoopInterface $loop |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
class Client |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
const VERSION = '4.0.7'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $token; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var LoggerInterface |
54
|
|
|
*/ |
55
|
|
|
protected $logger; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var LoopInterface |
59
|
|
|
*/ |
60
|
|
|
protected $loop; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
private $storage = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
private $retries = 100; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var HttpAdapter|null |
74
|
|
|
*/ |
75
|
|
|
private $http; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var StreamAdapter|null |
79
|
|
|
*/ |
80
|
|
|
private $streaming; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Client constructor. |
84
|
|
|
* @param string $token |
85
|
|
|
* @param LoggerInterface $logger |
86
|
|
|
*/ |
87
|
|
|
public function __construct(string $token, LoggerInterface $logger = null) |
88
|
|
|
{ |
89
|
|
|
$this->token = $token; |
90
|
|
|
$this->loop = Factory::create(); |
91
|
|
|
|
92
|
|
|
if (null === ($this->logger = $logger)) { |
93
|
|
|
$this->logger = new NullLogger(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $token |
99
|
|
|
* @return Client |
100
|
|
|
*/ |
101
|
|
|
public function updateToken(string $token): Client |
102
|
|
|
{ |
103
|
|
|
$this->token = $token; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int $count |
110
|
|
|
* @return Client |
111
|
|
|
*/ |
112
|
|
|
public function retries(int $count): Client |
113
|
|
|
{ |
114
|
|
|
$this->retries = $count; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getRetriesCount(): int |
123
|
|
|
{ |
124
|
|
|
return $this->retries; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function clear() |
131
|
|
|
{ |
132
|
|
|
$this->loop->stop(); |
133
|
|
|
$this->storage = []; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return SyncAdapterInterface|AdapterInterface |
138
|
|
|
* @throws \InvalidArgumentException |
139
|
|
|
*/ |
140
|
|
|
public function viaHttp(): SyncAdapterInterface |
141
|
|
|
{ |
142
|
|
|
if ($this->http === null) { |
143
|
|
|
$this->http = new HttpAdapter($this); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $this->http; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return StreamAdapterInterface|AdapterInterface |
151
|
|
|
*/ |
152
|
|
|
public function viaStream(): StreamAdapterInterface |
153
|
|
|
{ |
154
|
|
|
if ($this->streaming === null) { |
155
|
|
|
$this->streaming = new StreamAdapter($this, $this->loop); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this->streaming; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function connect() |
165
|
|
|
{ |
166
|
|
|
$this->loop->run(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $hookId |
171
|
|
|
* @return WebHook |
172
|
|
|
* @throws \InvalidArgumentException |
173
|
|
|
*/ |
174
|
|
|
public function notify(string $hookId): WebHook |
175
|
|
|
{ |
176
|
|
|
return new WebHook($this, $hookId); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $resource |
181
|
|
|
* @return Groups|Messages|Rooms|Users|null|LoggerInterface|LoopInterface|string |
182
|
|
|
*/ |
183
|
|
|
public function __get(string $resource) |
184
|
|
|
{ |
185
|
|
|
$resolve = function (string $resource) { |
186
|
|
|
switch ($resource) { |
187
|
|
|
// == RESOURCES == |
188
|
|
|
case 'users': |
189
|
|
|
return new Users($this); |
190
|
|
|
case 'groups': |
191
|
|
|
return new Groups($this); |
192
|
|
|
case 'messages': |
193
|
|
|
return new Messages($this); |
194
|
|
|
case 'rooms': |
195
|
|
|
return new Rooms($this); |
196
|
|
|
|
197
|
|
|
// == COMMON === |
198
|
|
|
case 'loop': |
199
|
|
|
return $this->loop(); |
200
|
|
|
case 'token': |
201
|
|
|
return $this->token(); |
202
|
|
|
case 'logger': |
203
|
|
|
return $this->logger(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return null; |
207
|
|
|
}; |
208
|
|
|
|
209
|
|
|
if (! isset($this->storage[$resource])) { |
210
|
|
|
$this->storage[$resource] = $resolve($resource); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $this->storage[$resource]; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param string $name |
218
|
|
|
* @param $value |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
|
|
public function __set(string $name, $value) |
222
|
|
|
{ |
223
|
|
|
switch ($name) { |
224
|
|
|
// == COMMON === |
225
|
|
|
case 'loop': |
226
|
|
|
$this->loop($value); |
227
|
|
|
break; |
228
|
|
|
|
229
|
|
|
case 'token': |
230
|
|
|
$this->token($value); |
231
|
|
|
break; |
232
|
|
|
|
233
|
|
|
case 'logger': |
234
|
|
|
$this->logger($value); |
235
|
|
|
break; |
236
|
|
|
|
237
|
|
|
default: |
238
|
|
|
$this->{$name} = $value; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param LoopInterface|null $loop |
244
|
|
|
* @return LoopInterface |
245
|
|
|
*/ |
246
|
|
|
public function loop(LoopInterface $loop = null): LoopInterface |
247
|
|
|
{ |
248
|
|
|
if ($loop !== null) { |
249
|
|
|
$this->loop = $loop; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this->loop; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param string|null $token |
257
|
|
|
* @return string |
258
|
|
|
*/ |
259
|
|
|
public function token(string $token = null): string |
260
|
|
|
{ |
261
|
|
|
if ($token !== null) { |
262
|
|
|
$this->token = $token; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $this->token; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param LoggerInterface|null $logger |
270
|
|
|
* @return LoggerInterface |
271
|
|
|
*/ |
272
|
|
|
public function logger(LoggerInterface $logger = null): LoggerInterface |
273
|
|
|
{ |
274
|
|
|
if ($logger !== null) { |
275
|
|
|
$this->logger = $logger; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $this->logger; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
* @throws \Throwable |
284
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
285
|
|
|
* @throws \Exception |
286
|
|
|
* @throws \RuntimeException |
287
|
|
|
* @throws \InvalidArgumentException |
288
|
|
|
*/ |
289
|
|
|
public function authUser(): array |
290
|
|
|
{ |
291
|
|
|
return $this->users->current(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return string |
296
|
|
|
* @throws \Throwable |
297
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
298
|
|
|
* @throws \Exception |
299
|
|
|
* @throws \RuntimeException |
300
|
|
|
* @throws \InvalidArgumentException |
301
|
|
|
*/ |
302
|
|
|
public function authId(): string |
303
|
|
|
{ |
304
|
|
|
return $this->users->currentUserId(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param string $name |
309
|
|
|
* @throws \LogicException |
310
|
|
|
*/ |
311
|
|
|
public function __unset(string $name) |
312
|
|
|
{ |
313
|
|
|
switch ($name) { |
314
|
|
|
case 'logger': |
315
|
|
|
$this->logger = null; |
316
|
|
|
break; |
317
|
|
|
case 'loop': |
318
|
|
|
throw new \LogicException('Can not remove EventLoop'); |
319
|
|
|
case 'token': |
320
|
|
|
throw new \LogicException('Can not remove token value.'); |
321
|
|
|
case 'users': |
322
|
|
|
case 'groups': |
323
|
|
|
case 'messages': |
324
|
|
|
case 'rooms': |
325
|
|
|
throw new \LogicException('Resource ' . $name . ' can not be removed'); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param string $name |
331
|
|
|
* @return bool |
332
|
|
|
*/ |
333
|
|
|
public function __isset(string $name): bool |
334
|
|
|
{ |
335
|
|
|
if (\in_array($name, ['users', 'groups', 'messages', 'rooms', 'loop', 'token'], true)) { |
336
|
|
|
return true; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
if ($name === 'logger') { |
340
|
|
|
return $this->logger !== null; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return property_exists($this, $name) && $this->{$name} !== null; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|