|
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\ResourceInterface; |
|
18
|
|
|
use Gitter\Resources\Rooms; |
|
19
|
|
|
use Gitter\Resources\Users; |
|
20
|
|
|
use Gitter\Support\Loggable; |
|
21
|
|
|
use Monolog\Logger; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Psr\Log\NullLogger; |
|
24
|
|
|
use React\EventLoop\Factory; |
|
25
|
|
|
use React\EventLoop\LoopInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Client |
|
29
|
|
|
* @package Gitter |
|
30
|
|
|
*/ |
|
31
|
|
|
class Client implements Loggable |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
const VERSION = '4.0.0'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $token; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var LoggerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $logger; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var LoopInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $loop; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $storage = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Client constructor. |
|
60
|
|
|
* @param string $token |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct(string $token, LoggerInterface $logger = null) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->token = $token; |
|
65
|
|
|
$this->loop = Factory::create(); |
|
66
|
|
|
|
|
67
|
|
|
if (null === ($this->logger = $logger)) { |
|
68
|
|
|
$this->logger = new NullLogger(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $message |
|
74
|
|
|
* @param int $level |
|
75
|
|
|
* @return Loggable|$this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function log(string $message, int $level = Logger::INFO): Loggable |
|
78
|
|
|
{ |
|
79
|
|
|
$this->logger->log($level, $message); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param LoggerInterface|null $logger |
|
86
|
|
|
* @return LoggerInterface |
|
87
|
|
|
*/ |
|
88
|
|
|
public function logger(LoggerInterface $logger = null): LoggerInterface |
|
89
|
|
|
{ |
|
90
|
|
|
if ($logger !== null) { |
|
91
|
|
|
$this->logger = $logger; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $this->logger; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string|null $token |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function token(string $token = null): string |
|
102
|
|
|
{ |
|
103
|
|
|
if ($token !== null) { |
|
104
|
|
|
$this->token = $token; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->token; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return SyncAdapterInterface|AdapterInterface |
|
112
|
|
|
*/ |
|
113
|
|
|
public function viaHttp(): SyncAdapterInterface |
|
114
|
|
|
{ |
|
115
|
|
|
return new HttpAdapter($this); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return StreamAdapterInterface|AdapterInterface |
|
120
|
|
|
*/ |
|
121
|
|
|
public function viaStream(): StreamAdapterInterface |
|
122
|
|
|
{ |
|
123
|
|
|
return new StreamAdapter($this, $this->loop); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param LoopInterface|null $loop |
|
128
|
|
|
* @return LoopInterface |
|
129
|
|
|
*/ |
|
130
|
|
|
public function loop(LoopInterface $loop = null): LoopInterface |
|
131
|
|
|
{ |
|
132
|
|
|
if ($loop !== null) { |
|
133
|
|
|
$this->loop = $loop; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->loop; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function connect() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->loop->run(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return Groups |
|
149
|
|
|
*/ |
|
150
|
|
|
public function groups(): Groups |
|
151
|
|
|
{ |
|
152
|
|
|
return new Groups($this); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return Messages |
|
157
|
|
|
*/ |
|
158
|
|
|
public function messages(): Messages |
|
159
|
|
|
{ |
|
160
|
|
|
return new Messages($this); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return Rooms |
|
165
|
|
|
*/ |
|
166
|
|
|
public function rooms(): Rooms |
|
167
|
|
|
{ |
|
168
|
|
|
return new Rooms($this); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return Users |
|
173
|
|
|
*/ |
|
174
|
|
|
public function users(): Users |
|
175
|
|
|
{ |
|
176
|
|
|
return new Users($this); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $hookId |
|
181
|
|
|
* @return WebHook |
|
182
|
|
|
* @throws \InvalidArgumentException |
|
183
|
|
|
*/ |
|
184
|
|
|
public function notify(string $hookId): WebHook |
|
185
|
|
|
{ |
|
186
|
|
|
return new WebHook($this, $hookId); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|