Test Failed
Push — master ( 600e01...d11dc7 )
by Kirill
06:38
created

Client::updateToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
use Serafim\Evacuator\Evacuator;
26
27
/**
28
 * Class Client
29
 * @package Gitter
30
 *
31
 * @property-read Rooms $rooms
32
 * @property-read Users $users
33
 * @property-read Messages $messages
34
 * @property-read Groups $groups
35
 *
36
 * @property string $token
37
 * @property LoggerInterface|null $logger
38
 * @property LoopInterface $loop
39
 *
40
 */
41
class Client
42
{
43
    /**
44
     * @var string
45
     */
46
    const VERSION = '4.0.6';
47
48
    /**
49
     * @var string
50
     */
51
    protected $token;
52
53
    /**
54
     * @var LoggerInterface
55
     */
56
    protected $logger;
57
58
    /**
59
     * @var LoopInterface
60
     */
61
    protected $loop;
62
63
    /**
64
     * @var array
65
     */
66
    private $storage = [];
67
68
    /**
69
     * @var int
70
     */
71
    private $retries = 100;
72
73
    /**
74
     * @var HttpAdapter|null
75
     */
76
    private $http;
77
78
    /**
79
     * @var StreamAdapter|null
80
     */
81
    private $streaming;
82
83
    /**
84
     * Client constructor.
85
     * @param string $token
86
     * @param LoggerInterface $logger
87
     */
88
    public function __construct(string $token, LoggerInterface $logger = null)
89
    {
90
        $this->token = $token;
91
        $this->loop = Factory::create();
92
93
        if (null === ($this->logger = $logger)) {
94
            $this->logger = new NullLogger();
95
        }
96
    }
97
98
    /**
99
     * @param string $token
100
     * @return Client
101
     */
102
    public function updateToken(string $token): Client
103
    {
104
        $this->token = $token;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param int $count
111
     * @return Client
112
     */
113
    public function retries(int $count): Client
114
    {
115
        $this->retries = $count;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getRetriesCount(): int
124
    {
125
        return $this->retries;
126
    }
127
128
    /**
129
     * @return void
130
     */
131
    public function clear()
132
    {
133
        $this->loop->stop();
134
        $this->storage = [];
135
    }
136
137
    /**
138
     * @return SyncAdapterInterface|AdapterInterface
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
     * @return array
244
     * @throws \Throwable
245
     * @throws \GuzzleHttp\Exception\ClientException
246
     * @throws \Exception
247
     * @throws \RuntimeException
248
     * @throws \InvalidArgumentException
249
     */
250
    public function authUser(): array
251
    {
252
        return $this->users->current();
253
    }
254
255
    /**
256
     * @return string
257
     * @throws \Throwable
258
     * @throws \GuzzleHttp\Exception\ClientException
259
     * @throws \Exception
260
     * @throws \RuntimeException
261
     * @throws \InvalidArgumentException
262
     */
263
    public function authId(): string
264
    {
265
        return $this->users->currentUserId();
266
    }
267
268
    /**
269
     * @param LoopInterface|null $loop
270
     * @return LoopInterface
271
     */
272
    public function loop(LoopInterface $loop = null): LoopInterface
273
    {
274
        if ($loop !== null) {
275
            $this->loop = $loop;
276
        }
277
278
        return $this->loop;
279
    }
280
281
    /**
282
     * @param string|null $token
283
     * @return string
284
     */
285
    public function token(string $token = null): string
286
    {
287
        if ($token !== null) {
288
            $this->token = $token;
289
        }
290
291
        return $this->token;
292
    }
293
294
    /**
295
     * @param LoggerInterface|null $logger
296
     * @return LoggerInterface
297
     */
298
    public function logger(LoggerInterface $logger = null): LoggerInterface
299
    {
300
        if ($logger !== null) {
301
            $this->logger = $logger;
302
        }
303
304
        return $this->logger;
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
        } elseif ($name === 'logger') {
338
            return $this->logger !== null;
339
        }
340
341
        return property_exists($this, $name) && $this->{$name} !== null;
342
    }
343
}
344