1 | <?php |
||
17 | class Client |
||
18 | { |
||
19 | use ConfigTrait; |
||
20 | use LogTrait; |
||
21 | |||
22 | /** @var RealTimeClient */ |
||
23 | private $realTimeClient; |
||
24 | |||
25 | /** @var Users */ |
||
26 | private $users; |
||
27 | |||
28 | /** @var Channels */ |
||
29 | private $channels; |
||
30 | |||
31 | /** @var User */ |
||
32 | protected $authedUser; |
||
33 | |||
34 | public function __construct( |
||
47 | |||
48 | public function init() : Client |
||
49 | { |
||
50 | $this->initChannelUpdateHandlers(); |
||
51 | $this->initUserUpdateHandlers(); |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | public function update(Closure $authedUserUpdated) |
||
57 | { |
||
58 | $this->updateUsers(); |
||
59 | $this->updateChannels(); |
||
60 | $this->updateAuthedUser($authedUserUpdated); |
||
61 | } |
||
62 | |||
63 | public function onEvent($event, array $onEvent) |
||
64 | { |
||
65 | $this->realTimeClient->on($event, function(Payload $payload) use ($onEvent) { |
||
66 | call_user_func($onEvent, $payload); |
||
67 | }); |
||
68 | } |
||
69 | |||
70 | public function getRealTimeClient() |
||
71 | { |
||
72 | return $this->realTimeClient; |
||
73 | } |
||
74 | |||
75 | public function getAuthedUser() |
||
76 | { |
||
77 | return $this->authedUser; |
||
78 | } |
||
79 | |||
80 | public function getAuthedUsername() |
||
81 | { |
||
82 | return $this->authedUser->getUsername(); |
||
83 | } |
||
84 | |||
85 | public function connect() : PromiseInterface |
||
86 | { |
||
87 | return $this->realTimeClient->connect(); |
||
88 | } |
||
89 | |||
90 | public function disconnect() |
||
91 | { |
||
92 | return $this->realTimeClient->disconnect(); |
||
93 | } |
||
94 | |||
95 | public function say($text, $channelOrName, array $additionalParameters = []) |
||
96 | { |
||
97 | $channel = $channelOrName; |
||
98 | if (!($channel instanceof ChannelInterface)) { |
||
99 | $channel = $this->getChannelByName($channelOrName); |
||
100 | if (!($channel instanceof ChannelInterface)) { |
||
101 | $channel = $this->getChannelById($channelOrName); |
||
102 | if (!($channel instanceof ChannelInterface)) { |
||
103 | $this->warning('No channel, trying to say: '.$text); |
||
104 | return; |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (empty($additionalParameters) && $this->useWebSocket()) { |
||
110 | // WebSocket send does not support message formatting. |
||
111 | $this->send($text, $channel); |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | // Http post send supports message formatting. |
||
116 | $this->post($text, $channel, $additionalParameters); |
||
117 | } |
||
118 | |||
119 | public function send($text, ChannelInterface $channel) |
||
120 | { |
||
121 | $this->realTimeClient->send($text, $channel); |
||
122 | } |
||
123 | |||
124 | public function post($text, ChannelInterface $channel, array $additionalParameters = []) |
||
125 | { |
||
126 | $parameters = array_merge([ |
||
127 | 'text' => $text, |
||
128 | 'channel' => $channel->getId(), |
||
129 | 'as_user' => true, |
||
130 | ], $additionalParameters); |
||
131 | |||
132 | $this->realTimeClient->apiCall('chat.postMessage', $parameters); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param $id |
||
137 | * @return null|User |
||
138 | */ |
||
139 | public function getUserById($id) |
||
140 | { |
||
141 | return $this->users->byId($id); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param $name |
||
146 | * @return null|User |
||
147 | */ |
||
148 | public function getUserByName($name) |
||
149 | { |
||
150 | return $this->users->byName($name); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param $id |
||
155 | * @return null|Channel |
||
156 | */ |
||
157 | public function getChannelById($id) |
||
158 | { |
||
159 | return $this->channels->byId($id); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param $name |
||
164 | * @return null|Channel |
||
165 | */ |
||
166 | public function getChannelByName($name) |
||
167 | { |
||
168 | return $this->channels->byName($name); |
||
169 | } |
||
170 | |||
171 | protected function useWebSocket() : bool |
||
172 | { |
||
173 | return (bool) $this->get('use.websocket', false); |
||
174 | } |
||
175 | |||
176 | protected function updateUsers() |
||
182 | |||
183 | protected function updateChannels() |
||
189 | |||
190 | protected function updateAuthedUser(Closure $authedUserUpdated) |
||
197 | |||
198 | protected function initChannelUpdateHandlers() |
||
205 | |||
206 | protected function initUserUpdateHandlers() |
||
213 | } |