Completed
Pull Request — master (#6)
by Dan
01:33
created

Client::getBotsMap()   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 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 = $channelOrName;
116
        if (!($channel instanceof ChannelInterface)) {
117
            $channel = $this->getChannelByName($channelOrName);
118
            if (!($channel instanceof ChannelInterface)) {
119
                $channel = $this->getChannelById($channelOrName);
120
                if (!($channel instanceof ChannelInterface)) {
121
                    $this->warning('No channel, trying to say: '.$text);
122
                    return;
123
                }
124
            }
125
        }
126
127
        if (empty($additionalParameters) && $this->useWebSocket()) {
128
            // WebSocket send does not support message formatting.
129
            $this->send($text, $channel);
130
            return;
131
        }
132
133
        // Http post send supports message formatting.
134
        $this->post($text, $channel, $additionalParameters);
135
    }
136
137
    public function send($text, ChannelInterface $channel)
138
    {
139
        $this->realTimeClient->send($text, $channel);
140
    }
141
142 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...
143
    {
144
        $parameters = array_merge([
145
            'text' => $text,
146
            'channel' => $channel->getId(),
147
            'as_user' => true,
148
        ], $additionalParameters);
149
150
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
151
    }
152
153 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...
154
    {
155
        $parameters = [
156
            'text' => $text,
157
            'channel' => $userName,
158
            'as_user' => false,
159
        ];
160
161
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
162
    }
163
164
165
    public function getUsersMap() : array
166
    {
167
        return $this->users->getMap();
168
    }
169
170
    public function getBotsMap() : array
171
    {
172
        return $this->bots->getMap();
173
    }
174
175
    public function getChannelsMap() : array
176
    {
177
        return $this->channels->getMap();
178
    }
179
180
    /**
181
     * @param $id
182
     * @return null|User
183
     */
184
    public function getUserById($id)
185
    {
186
        return $this->users->byId($id);
187
    }
188
189
    /**
190
     * @param $name
191
     * @return null|User
192
     */
193
    public function getUserByName($name)
194
    {
195
        return $this->users->byName($name);
196
    }
197
198
    /**
199
     * @param $id
200
     * @return null|Bot
201
     */
202
    public function getBotById($id)
203
    {
204
        return $this->bots->byId($id);
205
    }
206
207
    /**
208
     * @param $name
209
     * @return null|Bot
210
     */
211
    public function getBotByName($name)
212
    {
213
        return $this->bots->byName($name);
214
    }
215
216
    /**
217
     * @param $id
218
     * @return null|Channel
219
     */
220
    public function getChannelById($id)
221
    {
222
        return $this->channels->byId($id);
223
    }
224
225
    /**
226
     * @param $name
227
     * @return null|Channel
228
     */
229
    public function getChannelByName($name)
230
    {
231
        return $this->channels->byName($name);
232
    }
233
234
    public function updateUsers()
235
    {
236
        $this->realTimeClient->getUsers()->then(function(array $users) {
237
            $this->users->update($users);
238
        });
239
    }
240
241
    public function updateBots()
242
    {
243
        $this->realTimeClient->getBots()->then(function(array $bots) {
244
            $this->bots->update($bots);
245
        });
246
    }
247
248
    public function updateChannels()
249
    {
250
        $this->realTimeClient->getChannels()->then(function(array $channels) {
251
            $this->channels->update($channels);
252
        });
253
    }
254
255
    protected function updateAuthedUser(Closure $authedUserUpdated)
256
    {
257
        $this->realTimeClient->getAuthedUser()->then(function(User $user) use ($authedUserUpdated) {
258
            $this->authedUser = $user;
259
            $authedUserUpdated($user);
260
        });
261
    }
262
263
    protected function initChannelUpdateHandlers()
264
    {
265
        $events = ['channel_created', 'channel_deleted', 'channel_rename'];
266
        foreach ($events as $event) {
267
            $this->onEvent($event, [$this, 'updateChannels']);
268
        }
269
    }
270
271
    protected function initUserUpdateHandlers()
272
    {
273
        $events = ['user_change'];
274
        foreach ($events as $event) {
275
            $this->onEvent($event, [$this, 'updateUsers']);
276
        }
277
    }
278
279
    protected function useWebSocket() : bool
280
    {
281
        return (bool) $this->get('use.websocket', false);
282
    }
283
}