Event::isDirectMessage()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.2728
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 5
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\client\ApiClient;
6
7
class Event extends AbstractBaseSlack
8
{
9
    private $type;
10
    private $user;
11
    private $text;
12
    private $timestamp;
13
    private $eventTimestamp;
14
    private $channel;
15
    private $botId;
16
17
    /**
18
     * Dependencies.
19
     */
20
    private $apiClient;
21
22
    /**
23
     * Event constructor.
24
     *
25
     * @param $type
26
     */
27 13
    public function __construct($type)
28
    {
29 13
        $this->setType($type);
30 13
    }
31
32
    /**
33
     * @return string
34
     */
35 4
    public function getType(): string
36
    {
37 4
        return $this->type;
38
    }
39
40
    /**
41
     * @param string $type
42
     */
43 13
    public function setType(string $type)
44
    {
45 13
        $this->type = $type;
46 13
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function getUser(): string
52
    {
53 1
        return $this->user;
54
    }
55
56
    /**
57
     * @param string $user
58
     */
59 6
    public function setUser(string $user)
60
    {
61 6
        $this->user = $user;
62 6
    }
63
64
    /**
65
     * @return string
66
     */
67 3
    public function getText()
68
    {
69 3
        return $this->text;
70
    }
71
72
    /**
73
     * @param string $text
74
     */
75 7
    public function setText(string $text)
76
    {
77 7
        $this->text = $text;
78 7
    }
79
80
    /**
81
     * @return string
82
     */
83 2
    public function getTimestamp(): string
84
    {
85 2
        return $this->timestamp;
86
    }
87
88
    /**
89
     * @param string $timestamp
90
     */
91 7
    public function setTimestamp(string $timestamp)
92
    {
93 7
        $this->timestamp = $timestamp;
94 7
    }
95
96
    /**
97
     * @return string
98
     */
99 2
    public function getEventTimestamp(): string
100
    {
101 2
        return $this->eventTimestamp;
102
    }
103
104
    /**
105
     * @param string $eventTimestamp
106
     */
107 7
    public function setEventTimestamp(string $eventTimestamp)
108
    {
109 7
        $this->eventTimestamp = $eventTimestamp;
110 7
    }
111
112
    /**
113
     * @return string
114
     */
115 4
    public function getChannel()
116
    {
117 4
        return $this->channel;
118
    }
119
120
    /**
121
     * @param string $channel
122
     */
123 7
    public function setChannel(string $channel)
124
    {
125 7
        $this->channel = $channel;
126 7
    }
127
128
    /**
129
     * @return string
130
     */
131 4
    public function getBotId()
132
    {
133 4
        return $this->botId;
134
    }
135
136
    /**
137
     * @param string $botId
138
     */
139 3
    public function setBotId(string $botId)
140
    {
141 3
        $this->botId = $botId;
142 3
    }
143
144
    /**
145
     * @return ApiClient
146
     */
147 3
    public function getApiClient(): ApiClient
148
    {
149 3
        if (!isset($this->apiClient)) {
150 1
            $this->setApiClient(new ApiClient());
151
        }
152
153 3
        return $this->apiClient;
154
    }
155
156
    /**
157
     * @param ApiClient $apiClient
158
     */
159 3
    public function setApiClient(ApiClient $apiClient)
160
    {
161 3
        $this->apiClient = $apiClient;
162 3
    }
163
164
    /**
165
     * Check if the event belongs to a direct message.
166
     *
167
     * @throws \Exception
168
     *
169
     * @return bool|void
170
     */
171 3
    public function isDirectMessage()
172
    {
173 3
        $imChannels = $this->getApiClient()->imListAsObject();
174
175 3
        if (empty($imChannels)) {
176 1
            return;
177
        }
178
179 2
        foreach ($imChannels as $imChannel) {
180
            /** @var ImChannel $imChannel */
181 2
            if ($imChannel->getUser() === 'USLACKBOT') {
182
                // ignore any direct conversation with the default slack bot
183 2
                continue;
184
            }
185
186
            // if IM Object id equals the event channel id the conversation is with the bot
187 2
            if ($imChannel->getSlackId() === $this->getChannel()) {
188 2
                return true;
189
            }
190
        }
191 1
    }
192
}
193