Completed
Push — master ( d233d7...a08bf1 )
by Kirill
02:31
created

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