Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

Sender::send()   B

Complexity

Conditions 9
Paths 25

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10.2655

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 21
cts 28
cp 0.75
rs 7.255
c 0
b 0
f 0
cc 9
eloc 32
nc 25
nop 3
crap 10.2655

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 /* @noinspection PhpUndefinedClassInspection */
6
    GuzzleHttp\Client;
7
use /* @noinspection PhpUndefinedClassInspection */
8
    GuzzleHttp\Psr7\Request;
9
use Botonomous\client\ApiClient;
10
use Botonomous\utility\LoggerUtility;
11
12
class Sender
13
{
14
    private $slackbot;
15
    private $loggerUtility;
16
    private $config;
17
18
    /**
19
     * Sender constructor.
20
     *
21
     * @param AbstractBot $slackbot
22
     */
23 9
    public function __construct(AbstractBot $slackbot)
24
    {
25 9
        $this->setSlackbot($slackbot);
26 9
    }
27
28
    /**
29
     * @return Slackbot
30
     */
31 8
    public function getSlackbot()
32
    {
33 8
        return $this->slackbot;
34
    }
35
36
    /**
37
     * @param AbstractBot $slackbot
38
     */
39 9
    public function setSlackbot(AbstractBot $slackbot)
40
    {
41 9
        $this->slackbot = $slackbot;
42 9
    }
43
44
    /**
45
     * Final endpoint for the response.
46
     *
47
     * @param $channel
48
     * @param $response
49
     * @param $attachments
50
     *
51
     * @throws \Exception
52
     *
53
     * @return bool
54
     */
55 8
    public function send($channel, $response, $attachments = null)
56
    {
57
        // @codeCoverageIgnoreStart
58
        if ($this->getSlackbot()->getListener()->isThisBot() !== false) {
59
            return false;
60
        }
61
        // @codeCoverageIgnoreEnd
62
63 8
        $responseType = $this->getConfig()->get('response');
64 8
        $debug = (bool) $this->getSlackbot()->getRequest('debug');
65
66 8
        if (empty($channel)) {
67 3
            $channel = $this->getConfig()->get('channel');
68
        }
69
70
        $data = [
71 8
            'text'    => $response,
72 8
            'channel' => $channel,
73
        ];
74
75 8
        if ($attachments !== null) {
76 1
            $data['attachments'] = json_encode($attachments);
77
        }
78
79 8
        if ($debug === true) {
80 3
            echo json_encode($data);
81 5
        } elseif ($responseType === 'slack') {
82 1
            $this->getLoggerUtility()->logChat(__METHOD__, $response);
83 1
            (new ApiClient())->chatPostMessage($data);
84 4
        } elseif ($responseType === 'slashCommand') {
85
            /** @noinspection PhpUndefinedClassInspection */
86
            $request = new Request(
87
                'POST',
88
                $this->getSlackbot()->getRequest('response_url'),
89
                ['Content-Type' => 'application/json'],
90
                json_encode([
91
                    'text'          => $response,
92
                    'response_type' => 'in_channel',
93
                ])
94
            );
95
96
            /* @noinspection PhpUndefinedClassInspection */
97
            (new Client())->send($request);
98 4
        } elseif ($responseType === 'json') {
99 4
            $this->getLoggerUtility()->logChat(__METHOD__, $response);
100
            // headers_sent is used to avoid issue in the test
101 4
            if (!headers_sent()) {
102 4
                header('Content-type:application/json;charset=utf-8');
103
            }
104 4
            echo json_encode($data);
105
        }
106
107 8
        return true;
108
    }
109
110
    /**
111
     * @return LoggerUtility
112
     */
113 5
    public function getLoggerUtility()
114
    {
115 5
        if (!isset($this->loggerUtility)) {
116 5
            $this->setLoggerUtility(new LoggerUtility());
117
        }
118
119 5
        return $this->loggerUtility;
120
    }
121
122
    /**
123
     * @param LoggerUtility $loggerUtility
124
     */
125 5
    public function setLoggerUtility(LoggerUtility $loggerUtility)
126
    {
127 5
        $this->loggerUtility = $loggerUtility;
128 5
    }
129
130
    /**
131
     * @return Config
132
     */
133 9
    public function getConfig()
134
    {
135 9
        if ($this->config === null) {
136 8
            $this->config = (new Config());
137
        }
138
139 9
        return $this->config;
140
    }
141
142
    /**
143
     * @param Config $config
144
     */
145 1
    public function setConfig(Config $config)
146
    {
147 1
        $this->config = $config;
148 1
    }
149
}
150