Passed
Push — master ( 27005d...e08107 )
by Ehsan
07:22
created

Sender   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 98.48%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 10
dl 0
loc 173
ccs 65
cts 66
cp 0.9848
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSlackbot() 0 4 1
A setSlackbot() 0 4 1
C send() 0 34 7
A respondToSlack() 0 4 1
A respondAsJSON() 0 9 2
A getResponseType() 0 10 3
A getResponseByListenerType() 0 12 3
A sendConfirmation() 0 19 3
A respondToSlashCommand() 0 16 1
1
<?php
2
3
namespace Botonomous;
4
5
use /* @noinspection PhpUndefinedClassInspection */
6
    GuzzleHttp\Psr7\Request;
7
8
class Sender extends AbstractSender
9
{
10
    private $slackbot;
11
12
    /**
13
     * Sender constructor.
14
     *
15
     * @param AbstractBot $slackbot
16
     */
17 13
    public function __construct(AbstractBot $slackbot)
18
    {
19 13
        $this->setSlackbot($slackbot);
20 13
    }
21
22
    /**
23
     * @return Slackbot
24
     */
25 9
    public function getSlackbot()
26
    {
27 9
        return $this->slackbot;
28
    }
29
30
    /**
31
     * @param AbstractBot $slackbot
32
     */
33 13
    public function setSlackbot(AbstractBot $slackbot)
34
    {
35 13
        $this->slackbot = $slackbot;
36 13
    }
37
38
    /**
39
     * Final endpoint for the response.
40
     *
41
     * @param $channel
42
     * @param $text
43
     * @param $attachments
44
     *
45
     * @throws \Exception
46
     *
47
     * @return bool
48
     */
49 9
    public function send($text, $channel = null, $attachments = null)
50
    {
51
        // @codeCoverageIgnoreStart
52
        if ($this->getSlackbot()->getListener()->isThisBot() !== false) {
53
            return false;
54
        }
55
        // @codeCoverageIgnoreEnd
56
57 9
        if (empty($channel)) {
58 3
            $channel = $this->getSlackbot()->getListener()->getChannelId();
59
        }
60
61
        $data = [
62 9
            'text'    => $text,
63 9
            'channel' => $channel,
64
        ];
65
66 9
        if ($attachments !== null) {
67 1
            $data['attachments'] = json_encode($attachments);
68
        }
69
70 9
        $this->getLoggerUtility()->logChat(__METHOD__, $text, $channel);
71
72 9
        $responseType = $this->getResponseType();
73 9
        if ($responseType === 'slack') {
74 1
            $this->respondToSlack($data);
75 8
        } elseif ($responseType === 'slashCommand') {
76 1
            $this->respondToSlashCommand($text);
77 7
        } elseif ($responseType === 'json') {
78 7
            $this->respondAsJSON($data);
79
        }
80
81 9
        return true;
82
    }
83
84
    /**
85
     * @param $response
86
     */
87 1
    private function respondToSlashCommand($response)
88
    {
89
        /** @noinspection PhpUndefinedClassInspection */
90 1
        $request = new Request(
91 1
            'POST',
92 1
            $this->getSlackbot()->getRequest('response_url'),
93 1
            ['Content-Type' => 'application/json'],
94 1
            json_encode([
95 1
                'text'          => $response,
96 1
                'response_type' => 'in_channel',
97
            ])
98
        );
99
100
        /* @noinspection PhpUndefinedClassInspection */
101 1
        $this->getClient()->send($request);
102 1
    }
103
104
    /**
105
     * @param $data
106
     */
107 1
    private function respondToSlack($data)
108
    {
109 1
        $this->getApiClient()->chatPostMessage($data);
110 1
    }
111
112
    /**
113
     * @param $data
114
     */
115 7
    private function respondAsJSON($data)
116
    {
117
        // headers_sent is used to avoid issue in the test
118 7
        if (!headers_sent()) {
119 7
            header('Content-type:application/json;charset=utf-8');
120
        }
121
122 7
        echo json_encode($data);
123 7
    }
124
125
    /**
126
     * Specify the response type
127
     * If response in config is set to empty, it will be considered based on listener.
128
     *
129
     * @return mixed|string
130
     */
131 9
    private function getResponseType()
132
    {
133 9
        if ($this->getSlackbot()->getRequest('debug') === true
134 9
            || $this->getSlackbot()->getRequest('debug') === 'true') {
135 7
            return 'json';
136
        }
137
138
        // response type in the config is empty, so choose it based on listener type
139 2
        return $this->getResponseByListenerType();
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145 2
    private function getResponseByListenerType()
146
    {
147 2
        $listener = $this->getConfig()->get('listener');
148
        switch ($listener) {
149 2
            case 'slashCommand':
150 1
                return 'slashCommand';
151 1
            case 'event':
152 1
                return 'slack';
153
            default:
154
                return 'slashCommand';
155
        }
156
    }
157
158
    /**
159
     * Send confirmation.
160
     */
161 1
    public function sendConfirmation()
162
    {
163 1
        $userId = $this->getSlackbot()->getRequest('user_id');
164
165 1
        $user = '';
166 1
        if (!empty($userId)) {
167 1
            $user = $this->getMessageUtility()->linkToUser($userId).' ';
168
        }
169
170 1
        $confirmMessage = $this->getSlackbot()->getDictionary()->getValueByKey(
171 1
            'generic-messages',
172 1
            'confirmReceivedMessage',
173 1
            ['user' => $user]
174
        );
175
176 1
        if (!empty($confirmMessage)) {
177 1
            $this->send($confirmMessage);
178
        }
179 1
    }
180
}
181