Passed
Push — master ( c8ed24...0a0550 )
by Ehsan
02:51
created

Sender::respondToSlashCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
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 $response
51
     * @param $attachments
52
     *
53
     * @throws \Exception
54
     *
55
     * @return bool
56
     */
57 7
    public function send($channel, $response, $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 7
        $debug = (bool) $this->getSlackbot()->getRequest('debug');
67
68 7
        if (empty($channel)) {
69 3
            $channel = $this->getConfig()->get('channel');
70
        }
71
72
        $data = [
73 7
            'text'    => $response,
74 7
            'channel' => $channel,
75
        ];
76
77 7
        if ($attachments !== null) {
78 1
            $data['attachments'] = json_encode($attachments);
79
        }
80
81 7
        $this->getLoggerUtility()->logChat(__METHOD__, $response, $channel);
82
83 7
        if ($responseType === 'slack') {
84 1
            $this->respondToSlack($data);
85 6
        } elseif ($responseType === 'slashCommand') {
86
            $this->respondToSlashCommand($response);
87 6
        } elseif ($responseType === 'json' || $debug === true) {
88 6
            $this->respondAsJSON($data);
89
        }
90
91 7
        return true;
92
    }
93
94
    /**
95
     * @param $response
96
     */
97
    private function respondToSlashCommand($response)
98
    {
99
        /** @noinspection PhpUndefinedClassInspection */
100
        $request = new Request(
101
            'POST',
102
            $this->getSlackbot()->getRequest('response_url'),
103
            ['Content-Type' => 'application/json'],
104
            json_encode([
105
                'text'          => $response,
106
                'response_type' => 'in_channel',
107
            ])
108
        );
109
110
        /* @noinspection PhpUndefinedClassInspection */
111
        $this->getClient()->send($request);
112
    }
113
114
    /**
115
     * @param $data
116
     */
117 1
    private function respondToSlack($data)
118
    {
119 1
        $this->getApiClient()->chatPostMessage($data);
120 1
    }
121
122
    /**
123
     * @param $data
124
     */
125 6
    private function respondAsJSON($data)
126
    {
127
        // headers_sent is used to avoid issue in the test
128 6
        if (!headers_sent()) {
129 6
            header('Content-type:application/json;charset=utf-8');
130
        }
131
132 6
        echo json_encode($data);
133 6
    }
134
135
    /**
136
     * @return LoggerUtility
137
     */
138 7
    public function getLoggerUtility()
139
    {
140 7
        if (!isset($this->loggerUtility)) {
141 7
            $this->setLoggerUtility(new LoggerUtility());
142
        }
143
144 7
        return $this->loggerUtility;
145
    }
146
147
    /**
148
     * @param LoggerUtility $loggerUtility
149
     */
150 7
    public function setLoggerUtility(LoggerUtility $loggerUtility)
151
    {
152 7
        $this->loggerUtility = $loggerUtility;
153 7
    }
154
155
    /**
156
     * @return Config
157
     */
158 8
    public function getConfig()
159
    {
160 8
        if (!isset($this->config)) {
161 7
            $this->config = (new Config());
162
        }
163
164 8
        return $this->config;
165
    }
166
167
    /**
168
     * @param Config $config
169
     */
170 1
    public function setConfig(Config $config)
171
    {
172 1
        $this->config = $config;
173 1
    }
174
175
    /**
176
     * @return ApiClient
177
     */
178 1
    public function getApiClient()
179
    {
180 1
        if (!isset($this->apiClient)) {
181 1
            $this->setApiClient(new ApiClient());
182
        }
183
184 1
        return $this->apiClient;
185
    }
186
187
    /**
188
     * @param ApiClient $apiClient
189
     */
190 1
    public function setApiClient(ApiClient $apiClient)
191
    {
192 1
        $this->apiClient = $apiClient;
193 1
    }
194
195
    /**
196
     * @return Client
197
     */
198 1
    public function getClient()
199
    {
200 1
        if (!isset($this->client)) {
201 1
            $this->setClient(new Client());
202
        }
203
204 1
        return $this->client;
205
    }
206
207
    /**
208
     * @param Client $client
209
     */
210 1
    public function setClient(Client $client)
211
    {
212 1
        $this->client = $client;
213 1
    }
214
215
    /**
216
     * Specify the response type
217
     * If response in config is set to empty, it will be considered based on listenerType
218
     *
219
     * @return mixed|string
220
     */
221 7
    private function getResponseType()
222
    {
223 7
        $responseType = $this->getConfig()->get('response');
224
225 7
        if (!empty($responseType)) {
226
            // Maybe later add a check for response type validation
227 7
            return $responseType;
228
        }
229
230
        // response type in the config is empty, so choose it based on listener type
231
232
        $listenerType = $this->getConfig()->get('listenerType');
233
        switch ($listenerType) {
234
            case 'slashCommand':
235
                return 'slashCommand';
236
            case 'event':
237
                return 'slack';
238
            default:
239
                return 'json';
240
        }
241
    }
242
}
243