Completed
Push — master ( b0e1f8...ce2c58 )
by Dan
01:40
created

Client::ping()   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\Channel;
12
use Slack\ChannelInterface;
13
use Slack\Payload;
14
use Slack\RealTimeClient;
15
use Slack\User;
16
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(
35
        RealTimeClient $realTimeClient,
36
        Users $users,
37
        Channels $channels,
38
        array $config = [],
39
        LoggerInterface $log = null)
40
    {
41
        $this->realTimeClient = $realTimeClient;
42
        $this->users = $users;
43
        $this->channels = $channels;
44
        $this->setConfig($config);
45
        $this->setLog($log);
46
    }
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
        $this->realTimeClient->disconnect();
93
    }
94
95
    public function reconnect() : PromiseInterface
96
    {
97
        $this->realTimeClient->disconnect();
98
        return $this->realTimeClient->connect();
99
    }
100
101
    public function ping() : PromiseInterface
102
    {
103
        return $this->getRealTimeClient()->ping();
104
    }
105
106
    public function say($text, $channelOrName, array $additionalParameters = [])
107
    {
108
        $channel = $channelOrName;
109
        if (!($channel instanceof ChannelInterface)) {
110
            $channel = $this->getChannelByName($channelOrName);
111
            if (!($channel instanceof ChannelInterface)) {
112
                $channel = $this->getChannelById($channelOrName);
113
                if (!($channel instanceof ChannelInterface)) {
114
                    $this->warning('No channel, trying to say: '.$text);
115
                    return;
116
                }
117
            }
118
        }
119
120
        if (empty($additionalParameters) && $this->useWebSocket()) {
121
            // WebSocket send does not support message formatting.
122
            $this->send($text, $channel);
123
            return;
124
        }
125
126
        // Http post send supports message formatting.
127
        $this->post($text, $channel, $additionalParameters);
128
    }
129
130
    public function send($text, ChannelInterface $channel)
131
    {
132
        $this->realTimeClient->send($text, $channel);
133
    }
134
135 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...
136
    {
137
        $parameters = array_merge([
138
            'text' => $text,
139
            'channel' => $channel->getId(),
140
            'as_user' => true,
141
        ], $additionalParameters);
142
143
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
144
    }
145
146 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...
147
    {
148
        $parameters = [
149
            'text' => $text,
150
            'channel' => $userName,
151
            'as_user' => false,
152
        ];
153
154
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
155
    }
156
157
    /**
158
     * @param $id
159
     * @return null|User
160
     */
161
    public function getUserById($id)
162
    {
163
        return $this->users->byId($id);
164
    }
165
166
    /**
167
     * @param $name
168
     * @return null|User
169
     */
170
    public function getUserByName($name)
171
    {
172
        return $this->users->byName($name);
173
    }
174
175
    /**
176
     * @param $id
177
     * @return null|Channel
178
     */
179
    public function getChannelById($id)
180
    {
181
        return $this->channels->byId($id);
182
    }
183
184
    /**
185
     * @param $name
186
     * @return null|Channel
187
     */
188
    public function getChannelByName($name)
189
    {
190
        return $this->channels->byName($name);
191
    }
192
193
    protected function useWebSocket() : bool
194
    {
195
        return (bool) $this->get('use.websocket', false);
196
    }
197
198
    protected function updateUsers()
199
    {
200
        $this->realTimeClient->getUsers()->then(function(array $users) {
201
            $this->users->update($users);
202
        });
203
    }
204
205
    protected function updateChannels()
206
    {
207
        $this->realTimeClient->getChannels()->then(function(array $channels) {
208
            $this->channels->update($channels);
209
        });
210
    }
211
212
    protected function updateAuthedUser(Closure $authedUserUpdated)
213
    {
214
        $this->realTimeClient->getAuthedUser()->then(function(User $user) use ($authedUserUpdated) {
215
            $this->authedUser = $user;
216
            $authedUserUpdated($user);
217
        });
218
    }
219
220
    protected function initChannelUpdateHandlers()
221
    {
222
        $events = ['channel_created', 'channel_deleted', 'channel_rename'];
223
        foreach ($events as $event) {
224
            $this->onEvent($event, [$this, 'updateChannels']);
225
        }
226
    }
227
228
    protected function initUserUpdateHandlers()
229
    {
230
        $events = ['user_change'];
231
        foreach ($events as $event) {
232
            $this->onEvent($event, [$this, 'updateUsers']);
233
        }
234
    }
235
}