Passed
Push — master ( 216cd9...9df751 )
by Ehsan
02:57
created

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