Passed
Push — master ( 4904f7...9a2e0d )
by Ehsan
02:54
created

Sender::setApiClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\client\ApiClient;
6
use Botonomous\utility\LoggerUtility;
7
use /* @noinspection PhpUndefinedClassInspection */
8
    GuzzleHttp\Client;
9
use /* @noinspection PhpUndefinedClassInspection */
10
    GuzzleHttp\Psr7\Request;
11
12
class Sender
13
{
14
    private $slackbot;
15
    private $loggerUtility;
16
    private $config;
17
    private $apiClient;
18
    private $client;
19
20
    /**
21
     * Sender constructor.
22
     *
23
     * @param AbstractBot $slackbot
24
     */
25 9
    public function __construct(AbstractBot $slackbot)
26
    {
27 9
        $this->setSlackbot($slackbot);
28 9
    }
29
30
    /**
31
     * @return Slackbot
32
     */
33 7
    public function getSlackbot()
34
    {
35 7
        return $this->slackbot;
36
    }
37
38
    /**
39
     * @param AbstractBot $slackbot
40
     */
41 9
    public function setSlackbot(AbstractBot $slackbot)
42
    {
43 9
        $this->slackbot = $slackbot;
44 9
    }
45
46
    /**
47
     * Final endpoint for the response.
48
     *
49
     * @param $channel
50
     * @param $text
51
     * @param $attachments
52
     *
53
     * @throws \Exception
54
     *
55
     * @return bool
56
     */
57 7
    public function send($text, $channel = null, $attachments = null)
58
    {
59
        // @codeCoverageIgnoreStart
60
        if ($this->getSlackbot()->getListener()->isThisBot() !== false) {
61
            return false;
62
        }
63
        // @codeCoverageIgnoreEnd
64
65 7
        $responseType = $this->getResponseType();
66
67 7
        if (empty($channel)) {
68 3
            $channel = $this->getSlackbot()->getListener()->getChannelId();
69
70 3
            if (empty($channel)) {
71 3
                $channel = $this->getConfig()->get('channel');
72
            }
73
        }
74
75
        $data = [
76 7
            'text'    => $text,
77 7
            'channel' => $channel,
78
        ];
79
80 7
        if ($attachments !== null) {
81 1
            $data['attachments'] = json_encode($attachments);
82
        }
83
84 7
        $this->getLoggerUtility()->logChat(__METHOD__, $text, $channel);
85
86 7
        if ($responseType === 'slack') {
87 1
            $this->respondToSlack($data);
88 6
        } elseif ($responseType === 'slashCommand') {
89
            $this->respondToSlashCommand($text);
90 6
        } elseif ($responseType === 'json') {
91 6
            $this->respondAsJSON($data);
92
        }
93
94 7
        return true;
95
    }
96
97
    /**
98
     * @param $response
99
     */
100
    private function respondToSlashCommand($response)
101
    {
102
        /** @noinspection PhpUndefinedClassInspection */
103
        $request = new Request(
104
            'POST',
105
            $this->getSlackbot()->getRequest('response_url'),
106
            ['Content-Type' => 'application/json'],
107
            json_encode([
108
                'text'          => $response,
109
                'response_type' => 'in_channel',
110
            ])
111
        );
112
113
        /* @noinspection PhpUndefinedClassInspection */
114
        $this->getClient()->send($request);
115
    }
116
117
    /**
118
     * @param $data
119
     */
120 1
    private function respondToSlack($data)
121
    {
122 1
        $this->getApiClient()->chatPostMessage($data);
123 1
    }
124
125
    /**
126
     * @param $data
127
     */
128 6
    private function respondAsJSON($data)
129
    {
130
        // headers_sent is used to avoid issue in the test
131 6
        if (!headers_sent()) {
132 6
            header('Content-type:application/json;charset=utf-8');
133
        }
134
135 6
        echo json_encode($data);
136 6
    }
137
138
    /**
139
     * @return LoggerUtility
140
     */
141 7
    public function getLoggerUtility()
142
    {
143 7
        if (!isset($this->loggerUtility)) {
144 7
            $this->setLoggerUtility(new LoggerUtility());
145
        }
146
147 7
        return $this->loggerUtility;
148
    }
149
150
    /**
151
     * @param LoggerUtility $loggerUtility
152
     */
153 7
    public function setLoggerUtility(LoggerUtility $loggerUtility)
154
    {
155 7
        $this->loggerUtility = $loggerUtility;
156 7
    }
157
158
    /**
159
     * @return Config
160
     */
161 6
    public function getConfig()
162
    {
163 6
        if (!isset($this->config)) {
164 5
            $this->config = (new Config());
165
        }
166
167 6
        return $this->config;
168
    }
169
170
    /**
171
     * @param Config $config
172
     */
173 1
    public function setConfig(Config $config)
174
    {
175 1
        $this->config = $config;
176 1
    }
177
178
    /**
179
     * @return ApiClient
180
     */
181 1
    public function getApiClient()
182
    {
183 1
        if (!isset($this->apiClient)) {
184 1
            $this->setApiClient(new ApiClient());
185
        }
186
187 1
        return $this->apiClient;
188
    }
189
190
    /**
191
     * @param ApiClient $apiClient
192
     */
193 1
    public function setApiClient(ApiClient $apiClient)
194
    {
195 1
        $this->apiClient = $apiClient;
196 1
    }
197
198
    /**
199
     * @return Client
200
     */
201 1
    public function getClient()
202
    {
203 1
        if (!isset($this->client)) {
204 1
            $this->setClient(new Client());
205
        }
206
207 1
        return $this->client;
208
    }
209
210
    /**
211
     * @param Client $client
212
     */
213 1
    public function setClient(Client $client)
214
    {
215 1
        $this->client = $client;
216 1
    }
217
218
    /**
219
     * Specify the response type
220
     * If response in config is set to empty, it will be considered based on listener.
221
     *
222
     * @return mixed|string
223
     */
224 7
    private function getResponseType()
225
    {
226 7
        if ($this->getSlackbot()->getRequest('debug') === true
227 7
            || $this->getSlackbot()->getRequest('debug') === 'true') {
228 2
            return 'json';
229
        }
230
231 5
        $responseType = $this->getConfig()->get('response');
232 5
        if (!empty($responseType)) {
233
            // Maybe later add a check for response type validation
234 5
            return $responseType;
235
        }
236
237
        // response type in the config is empty, so choose it based on listener type
238
        return $this->getResponseByListenerType();
239
    }
240
241
    /**
242
     * @return string|null
243
     */
244
    private function getResponseByListenerType()
245
    {
246
        $listener = $this->getConfig()->get('listener');
247
        switch ($listener) {
248
            case 'slashCommand':
249
                return 'slashCommand';
250
            case 'event':
251
                return 'slack';
252
        }
253
    }
254
}
255