1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Platform 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 Karma\System\Gitter; |
11
|
|
|
|
12
|
|
|
use Gitter\Client; |
13
|
|
|
use Karma\Platform\Ast\Transformer; |
14
|
|
|
use Karma\Platform\Io\AbstractSystem; |
15
|
|
|
use Karma\Platform\Io\ChannelInterface; |
16
|
|
|
use Karma\Platform\Io\UserInterface; |
17
|
|
|
use Karma\System\Gitter\Message\Parser; |
18
|
|
|
use Karma\System\Gitter\Message\Renderer; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use React\EventLoop\LoopInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class GitterSystem |
24
|
|
|
* @package Karma\System\Gitter |
25
|
|
|
*/ |
26
|
|
|
class GitterSystem extends AbstractSystem |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* System name |
30
|
|
|
*/ |
31
|
|
|
private const SYSTEM_NAME = 'gitter'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Client |
35
|
|
|
*/ |
36
|
|
|
private $client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var UserInterface|null |
40
|
|
|
*/ |
41
|
|
|
private $auth; |
42
|
|
|
|
43
|
|
|
private $transformer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* GitterSystem constructor. |
47
|
|
|
* @param string $token |
48
|
|
|
* @throws \DomainException |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $token) |
51
|
|
|
{ |
52
|
|
|
if (! class_exists(Client::class)) { |
53
|
|
|
throw new \DomainException('"serafim/gitter-api": "~4.0" required'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->client = new Client($token); |
57
|
|
|
$this->transformer = new Transformer(new Renderer(), new Parser()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Transformer |
62
|
|
|
*/ |
63
|
|
|
public function getTransformer(): Transformer |
64
|
|
|
{ |
65
|
|
|
return $this->transformer; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param LoopInterface $loop |
70
|
|
|
* @param null|LoggerInterface $logger |
71
|
|
|
*/ |
72
|
|
|
public function onRegister(LoopInterface $loop, ?LoggerInterface $logger): void |
73
|
|
|
{ |
74
|
|
|
$this->client->loop($loop); |
75
|
|
|
$this->client->logger($logger); |
76
|
|
|
|
77
|
|
|
parent::onRegister($loop, $logger); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Client |
82
|
|
|
*/ |
83
|
|
|
public function getClient(): Client |
84
|
|
|
{ |
85
|
|
|
return $this->client; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return UserInterface |
90
|
|
|
* @throws \Exception |
91
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
92
|
|
|
* @throws \InvalidArgumentException |
93
|
|
|
* @throws \RuntimeException |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
|
|
public function auth(): UserInterface |
97
|
|
|
{ |
98
|
|
|
if ($this->auth === null) { |
99
|
|
|
$data = $this->client->authUser(); |
100
|
|
|
|
101
|
|
|
$this->auth = $this->getUser($data['id'], function () use ($data) { |
102
|
|
|
return new GitterUser($this, $data); |
103
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->auth; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $channelId |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function has(string $channelId): bool |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
$this->channel($channelId); |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} catch (\Throwable $e) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $channelId |
126
|
|
|
* @return ChannelInterface |
127
|
|
|
* @throws \Throwable |
128
|
|
|
* @throws \RuntimeException |
129
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
130
|
|
|
* @throws \Exception |
131
|
|
|
* @throws \InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
public function channel(string $channelId): ChannelInterface |
134
|
|
|
{ |
135
|
|
|
return $this->getChannel($channelId, function () use ($channelId): ChannelInterface { |
136
|
|
|
$data = $this->client->rooms->join($channelId); |
137
|
|
|
|
138
|
|
|
return new GitterChannel($this, $data); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return \Traversable|ChannelInterface[]|GitterChannel[] |
144
|
|
|
* @throws \Exception |
145
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
146
|
|
|
* @throws \InvalidArgumentException |
147
|
|
|
* @throws \RuntimeException |
148
|
|
|
* @throws \Throwable |
149
|
|
|
*/ |
150
|
|
|
public function channels(): \Traversable |
151
|
|
|
{ |
152
|
|
|
foreach ($this->client->rooms->all() as $room) { |
153
|
|
|
$channel = new GitterChannel($this, $room); |
154
|
|
|
|
155
|
|
|
$this->push(ChannelInterface::class, $channel->getId(), $channel); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$channels = $this->identities(ChannelInterface::class); |
159
|
|
|
|
160
|
|
|
foreach ($channels as $channel) { |
161
|
|
|
yield $channel; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function __debugInfo(): array |
169
|
|
|
{ |
170
|
|
|
return [ |
171
|
|
|
'name' => $this->getName(), |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getName(): string |
179
|
|
|
{ |
180
|
|
|
return self::SYSTEM_NAME; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|