Completed
Push — master ( bdfdc7...772f65 )
by Kirill
02:27
created

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