|
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 Karma\Platform\Ast\NodeList; |
|
13
|
|
|
use Karma\Platform\Io\AbstractChannel; |
|
14
|
|
|
use Karma\Platform\Io\SystemInterface; |
|
15
|
|
|
use Karma\Platform\Io\MessageInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class GitterChannel |
|
19
|
|
|
* @package Karma\System\Gitter |
|
20
|
|
|
*/ |
|
21
|
|
|
class GitterChannel extends AbstractChannel |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* GitterChannel constructor. |
|
25
|
|
|
* @param SystemInterface|GitterSystem $system |
|
26
|
|
|
* @param array $data |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(SystemInterface $system, array $data) |
|
29
|
|
|
{ |
|
30
|
|
|
$name = $this->getChannelName($data); |
|
31
|
|
|
|
|
32
|
|
|
parent::__construct($system, $data['id'], $name); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $data |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
private function getChannelName(array $data): string |
|
40
|
|
|
{ |
|
41
|
|
|
switch (true) { |
|
42
|
|
|
case isset($data['url']): |
|
43
|
|
|
return substr($data['url'], 1); |
|
44
|
|
|
|
|
45
|
|
|
case isset($data['uri']): |
|
46
|
|
|
return $data['uri']; |
|
47
|
|
|
|
|
48
|
|
|
default: |
|
49
|
|
|
return $data['name'] ?? 'undefined'; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string|null $beforeId |
|
55
|
|
|
* @return \Traversable|MessageInterface[] |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
* @throws \GuzzleHttp\Exception\ClientException |
|
58
|
|
|
* @throws \InvalidArgumentException |
|
59
|
|
|
* @throws \RuntimeException |
|
60
|
|
|
* @throws \Throwable |
|
61
|
|
|
*/ |
|
62
|
|
|
public function messages(string $beforeId = null): \Traversable |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var GitterSystem $system */ |
|
65
|
|
|
$system = $this->system; |
|
66
|
|
|
|
|
67
|
|
|
$messages = $system->getClient()->messages->allBeforeId($this->id, $beforeId); |
|
68
|
|
|
|
|
69
|
|
|
foreach ($messages as $message) { |
|
70
|
|
|
yield new GitterMessage($this, $message); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param NodeList $nodes |
|
76
|
|
|
* @return MessageInterface |
|
77
|
|
|
* @throws \Exception |
|
78
|
|
|
* @throws \Throwable |
|
79
|
|
|
*/ |
|
80
|
|
|
public function publish(NodeList $nodes): MessageInterface |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var GitterSystem $system */ |
|
83
|
|
|
$system = $this->system; |
|
84
|
|
|
|
|
85
|
|
|
$message = $system->getTransformer()->render($nodes); |
|
86
|
|
|
$response = $system->getClient()->messages->create($this->getId(), $message); |
|
87
|
|
|
|
|
88
|
|
|
return new GitterMessage($this, $response); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param \Closure $then |
|
93
|
|
|
* @throws \Throwable |
|
94
|
|
|
*/ |
|
95
|
|
|
public function subscribe(\Closure $then): void |
|
96
|
|
|
{ |
|
97
|
|
|
/** @var GitterSystem $system */ |
|
98
|
|
|
$system = $this->system; |
|
99
|
|
|
|
|
100
|
|
|
$observer = $system->getClient()->rooms->messages($this->getId()); |
|
101
|
|
|
|
|
102
|
|
|
$observer->subscribe(function (array $data) use ($then) { |
|
103
|
|
|
$then(new GitterMessage($this, $data)); |
|
104
|
|
|
}); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __debugInfo(): array |
|
111
|
|
|
{ |
|
112
|
|
|
return [ |
|
113
|
|
|
'id' => $this->id, |
|
114
|
|
|
'name' => $this->name, |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|