Completed
Push — master ( aa45ca...3f2e9d )
by Dan
01:38
created

Client::updateBots()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Nopolabs\Yabot\Slack;
4
5
6
use Closure;
7
use Nopolabs\Yabot\Helpers\ConfigTrait;
8
use Nopolabs\Yabot\Helpers\LogTrait;
9
use Psr\Log\LoggerInterface;
10
use React\Promise\PromiseInterface;
11
use Slack\Bot;
12
use Slack\Channel;
13
use Slack\ChannelInterface;
14
use Slack\Payload;
15
use Slack\RealTimeClient;
16
use Slack\User;
17
18
class Client
19
{
20
    use ConfigTrait;
21
    use LogTrait;
22
23
    /** @var RealTimeClient */
24
    private $realTimeClient;
25
26
    /** @var Users */
27
    private $users;
28
29
    /** @var Bots */
30
    private $bots;
31
32
    /** @var Channels */
33
    private $channels;
34
35
    /** @var User */
36
    protected $authedUser;
37
38
    public function __construct(
39
        RealTimeClient $realTimeClient,
40
        Users $users,
41
        Bots $bots,
42
        Channels $channels,
43
        array $config = [],
44
        LoggerInterface $log = null)
45
    {
46
        $this->realTimeClient = $realTimeClient;
47
        $this->users = $users;
48
        $this->bots = $bots;
49
        $this->channels = $channels;
50
        $this->setConfig($config);
51
        $this->setLog($log);
52
    }
53
54
    public function init() : Client
55
    {
56
        $this->initChannelUpdateHandlers();
57
        $this->initUserUpdateHandlers();
58
59
        return $this;
60
    }
61
62
    public function update(Closure $authedUserUpdated)
63
    {
64
        $this->updateUsers();
65
        $this->updateBots();
66
        $this->updateChannels();
67
        $this->updateAuthedUser($authedUserUpdated);
68
    }
69
70
    public function onEvent($event, array $onEvent)
71
    {
72
        $this->realTimeClient->on($event, function(Payload $payload) use ($onEvent) {
73
            call_user_func($onEvent, $payload);
74
        });
75
    }
76
77
    public function getRealTimeClient()
78
    {
79
        return $this->realTimeClient;
80
    }
81
82
    public function getAuthedUser()
83
    {
84
        return $this->authedUser;
85
    }
86
87
    public function getAuthedUsername()
88
    {
89
        return $this->authedUser->getUsername();
90
    }
91
92
    public function connect() : PromiseInterface
93
    {
94
        return $this->realTimeClient->connect();
95
    }
96
97
    public function disconnect()
98
    {
99
        $this->realTimeClient->disconnect();
100
    }
101
102
    public function reconnect() : PromiseInterface
103
    {
104
        $this->realTimeClient->disconnect();
105
        return $this->realTimeClient->connect();
106
    }
107
108
    public function ping() : PromiseInterface
109
    {
110
        return $this->getRealTimeClient()->ping();
111
    }
112
113
    public function say($text, $channelOrName, array $additionalParameters = [])
114
    {
115
        $channel = $this->resolveChannel($channelOrName);
116
117
        if (!$channel) {
118
            $this->warning('No channel, trying to say: '.$text);
119
            return;
120
        }
121
122
        if (empty($additionalParameters) && $this->useWebSocket()) {
123
            // WebSocket send does not support message formatting.
124
            $this->send($text, $channel);
125
            return;
126
        }
127
128
        // Http post send supports message formatting.
129
        $this->post($text, $channel, $additionalParameters);
130
    }
131
132
    public function send($text, ChannelInterface $channel)
133
    {
134
        $this->realTimeClient->send($text, $channel);
135
    }
136
137 View Code Duplication
    public function post($text, ChannelInterface $channel, array $additionalParameters = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        $parameters = array_merge([
140
            'text' => $text,
141
            'channel' => $channel->getId(),
142
            'as_user' => true,
143
        ], $additionalParameters);
144
145
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
146
    }
147
148 View Code Duplication
    public function directMessage($text, $userName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $parameters = [
151
            'text' => $text,
152
            'channel' => $userName,
153
            'as_user' => false,
154
        ];
155
156
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
157
    }
158
159
160
    public function getUsersMap() : array
161
    {
162
        return $this->users->getMap();
163
    }
164
165
    public function getBotsMap() : array
166
    {
167
        return $this->bots->getMap();
168
    }
169
170
    public function getChannelsMap() : array
171
    {
172
        return $this->channels->getMap();
173
    }
174
175
    /**
176
     * @param $userId
177
     * @return null|User
178
     */
179
    public function getUserById($userId)
180
    {
181
        return $this->users->byId($userId);
182
    }
183
184
    /**
185
     * @param $name
186
     * @return null|User
187
     */
188
    public function getUserByName($name)
189
    {
190
        return $this->users->byName($name);
191
    }
192
193
    /**
194
     * @param $botId
195
     * @return null|Bot
196
     */
197
    public function getBotById($botId)
198
    {
199
        return $this->bots->byId($botId);
200
    }
201
202
    /**
203
     * @param $name
204
     * @return null|Bot
205
     */
206
    public function getBotByName($name)
207
    {
208
        return $this->bots->byName($name);
209
    }
210
211
    /**
212
     * @param $channelId
213
     * @return null|Channel
214
     */
215
    public function getChannelById($channelId)
216
    {
217
        return $this->channels->byId($channelId);
218
    }
219
220
    /**
221
     * @param $name
222
     * @return null|Channel
223
     */
224
    public function getChannelByName($name)
225
    {
226
        return $this->channels->byName($name);
227
    }
228
229
    public function updateUsers()
230
    {
231
        $this->realTimeClient->getUsers()->then(function(array $users) {
232
            $this->users->update($users);
233
        });
234
    }
235
236
    public function updateBots()
237
    {
238
        $this->realTimeClient->getBots()->then(function(array $bots) {
239
            $this->bots->update($bots);
240
        });
241
    }
242
243
    public function updateChannels()
244
    {
245
        $this->realTimeClient->getChannels()->then(function(array $channels) {
246
            $this->channels->update($channels);
247
        });
248
    }
249
250
    protected function updateAuthedUser(Closure $authedUserUpdated)
251
    {
252
        $this->realTimeClient->getAuthedUser()->then(function(User $user) use ($authedUserUpdated) {
253
            $this->authedUser = $user;
254
            $authedUserUpdated($user);
255
        });
256
    }
257
258
    protected function initChannelUpdateHandlers()
259
    {
260
        $events = ['channel_created', 'channel_deleted', 'channel_rename'];
261
        foreach ($events as $event) {
262
            $this->onEvent($event, [$this, 'updateChannels']);
263
        }
264
    }
265
266
    protected function initUserUpdateHandlers()
267
    {
268
        $events = ['user_change'];
269
        foreach ($events as $event) {
270
            $this->onEvent($event, [$this, 'updateUsers']);
271
        }
272
    }
273
274
    protected function useWebSocket() : bool
275
    {
276
        return (bool) $this->get('use.websocket', false);
277
    }
278
279
    private function resolveChannel($channelOrName)
280
    {
281
        $channel = $channelOrName;
282
        if ($channel instanceof ChannelInterface) {
283
            return $channel;
284
        }
285
286
        $channel = $this->getChannelByName($channelOrName);
287
        if ($channel instanceof ChannelInterface) {
288
            return $channel;
289
        }
290
291
        $channel = $this->getChannelById($channelOrName);
292
        if ($channel instanceof ChannelInterface) {
293
            return $channel;
294
        }
295
296
        return null;
297
    }
298
}