Completed
Push — master ( 1742b9...f39d89 )
by Ehsan
02:50
created

Sender::send()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 51
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10.0619

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 15
cts 22
cp 0.6818
rs 6.5978
cc 8
eloc 26
nc 17
nop 3
crap 10.0619

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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->getConfig()->get('response');
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
        if ($responseType === 'slack') {
82 1
            $this->getApiClient()->chatPostMessage($data);
83 6
        } elseif ($responseType === 'slashCommand') {
84
            /** @noinspection PhpUndefinedClassInspection */
85
            $request = new Request(
86
                'POST',
87
                $this->getSlackbot()->getRequest('response_url'),
88
                ['Content-Type' => 'application/json'],
89
                json_encode([
90
                    'text'          => $response,
91
                    'response_type' => 'in_channel',
92
                ])
93
            );
94
95
            /* @noinspection PhpUndefinedClassInspection */
96
            $this->getClient()->send($request);
97 6
        } elseif ($responseType === 'json' || $debug === true) {
98
            // headers_sent is used to avoid issue in the test
99
//            if (!headers_sent()) {
100
//                header('Content-type:application/json;charset=utf-8');
101
//            }
102
103 6
            echo json_encode($data);
104
        }
105
106 7
        return true;
107
    }
108
109
    /**
110
     * @return LoggerUtility
111
     */
112
    public function getLoggerUtility()
113
    {
114
        if (!isset($this->loggerUtility)) {
115
            $this->setLoggerUtility(new LoggerUtility());
116
        }
117
118
        return $this->loggerUtility;
119
    }
120
121
    /**
122
     * @param LoggerUtility $loggerUtility
123
     */
124
    public function setLoggerUtility(LoggerUtility $loggerUtility)
125
    {
126
        $this->loggerUtility = $loggerUtility;
127
    }
128
129
    /**
130
     * @return Config
131
     */
132 8
    public function getConfig()
133
    {
134 8
        if (!isset($this->config)) {
135 7
            $this->config = (new Config());
136
        }
137
138 8
        return $this->config;
139
    }
140
141
    /**
142
     * @param Config $config
143
     */
144 1
    public function setConfig(Config $config)
145
    {
146 1
        $this->config = $config;
147 1
    }
148
149
    /**
150
     * @return ApiClient
151
     */
152 1
    public function getApiClient()
153
    {
154 1
        if (!isset($this->apiClient)) {
155 1
            $this->setApiClient(new ApiClient());
156
        }
157
158 1
        return $this->apiClient;
159
    }
160
161
    /**
162
     * @param ApiClient $apiClient
163
     */
164 1
    public function setApiClient(ApiClient $apiClient)
165
    {
166 1
        $this->apiClient = $apiClient;
167 1
    }
168
169
    /**
170
     * @return Client
171
     */
172 1
    public function getClient()
173
    {
174 1
        if (!isset($this->client)) {
175 1
            $this->setClient(new Client());
176
        }
177
178 1
        return $this->client;
179
    }
180
181
    /**
182
     * @param Client $client
183
     */
184 1
    public function setClient(Client $client)
185
    {
186 1
        $this->client = $client;
187 1
    }
188
}
189