Completed
Push — master ( 065776...c5cb11 )
by Dan
01:53
created

Client   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 208
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 8
dl 20
loc 208
rs 10
c 0
b 0
f 0

23 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A init() 0 7 1
A update() 0 6 1
A onEvent() 0 6 1
A getRealTimeClient() 0 4 1
A getAuthedUser() 0 4 1
A getAuthedUsername() 0 4 1
A connect() 0 4 1
A disconnect() 0 4 1
B say() 0 23 6
A send() 0 4 1
A post() 10 10 1
A directMessage() 10 10 1
A getUserById() 0 4 1
A getUserByName() 0 4 1
A getChannelById() 0 4 1
A getChannelByName() 0 4 1
A useWebSocket() 0 4 1
A updateUsers() 0 6 1
A updateChannels() 0 6 1
A updateAuthedUser() 0 7 1
A initChannelUpdateHandlers() 0 7 2
A initUserUpdateHandlers() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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 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...
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 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...
136
    {
137
        $parameters = [
138
            'text' => $text,
139
            'channel' => $userName,
140
            'as_user' => false,
141
        ];
142
143
        $this->realTimeClient->apiCall('chat.postMessage', $parameters);
144
    }
145
146
    /**
147
     * @param $id
148
     * @return null|User
149
     */
150
    public function getUserById($id)
151
    {
152
        return $this->users->byId($id);
153
    }
154
155
    /**
156
     * @param $name
157
     * @return null|User
158
     */
159
    public function getUserByName($name)
160
    {
161
        return $this->users->byName($name);
162
    }
163
164
    /**
165
     * @param $id
166
     * @return null|Channel
167
     */
168
    public function getChannelById($id)
169
    {
170
        return $this->channels->byId($id);
171
    }
172
173
    /**
174
     * @param $name
175
     * @return null|Channel
176
     */
177
    public function getChannelByName($name)
178
    {
179
        return $this->channels->byName($name);
180
    }
181
182
    protected function useWebSocket() : bool
183
    {
184
        return (bool) $this->get('use.websocket', false);
185
    }
186
187
    protected function updateUsers()
188
    {
189
        $this->realTimeClient->getUsers()->then(function(array $users) {
190
            $this->users->update($users);
191
        });
192
    }
193
194
    protected function updateChannels()
195
    {
196
        $this->realTimeClient->getChannels()->then(function(array $channels) {
197
            $this->channels->update($channels);
198
        });
199
    }
200
201
    protected function updateAuthedUser(Closure $authedUserUpdated)
202
    {
203
        $this->realTimeClient->getAuthedUser()->then(function(User $user) use ($authedUserUpdated) {
204
            $this->authedUser = $user;
205
            $authedUserUpdated($user);
206
        });
207
    }
208
209
    protected function initChannelUpdateHandlers()
210
    {
211
        $events = ['channel_created', 'channel_deleted', 'channel_rename'];
212
        foreach ($events as $event) {
213
            $this->onEvent($event, [$this, 'updateChannels']);
214
        }
215
    }
216
217
    protected function initUserUpdateHandlers()
218
    {
219
        $events = ['user_change'];
220
        foreach ($events as $event) {
221
            $this->onEvent($event, [$this, 'updateUsers']);
222
        }
223
    }
224
}