Completed
Push — master ( 3fbfb5...43cfb4 )
by Kirill
116:33 queued 110:37
created

Client::getRetriesCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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