Completed
Push — master ( be594c...23bd25 )
by Ehsan
03:21
created

Sender   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 98.39%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 23
c 6
b 0
f 0
lcom 1
cbo 9
dl 0
loc 168
ccs 61
cts 62
cp 0.9839
rs 10

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 35 7
A respondToSlashCommand() 0 16 1
A respondToSlack() 0 4 1
A respondAsJSON() 0 9 2
A getResponseType() 0 10 3
A getResponseByListenerType() 0 10 3
A sendConfirmation() 0 15 3
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
        $responseType = $this->getResponseType();
58
59 9
        if (empty($channel)) {
60 3
            $channel = $this->getSlackbot()->getListener()->getChannelId();
61
        }
62
63
        $data = [
64 9
            'text'    => $text,
65 9
            'channel' => $channel,
66
        ];
67
68 9
        if ($attachments !== null) {
69 1
            $data['attachments'] = json_encode($attachments);
70
        }
71
72 9
        $this->getLoggerUtility()->logChat(__METHOD__, $text, $channel);
73
74 9
        if ($responseType === 'slack') {
75 1
            $this->respondToSlack($data);
76 8
        } elseif ($responseType === 'slashCommand') {
77 1
            $this->respondToSlashCommand($text);
78 7
        } elseif ($responseType === 'json') {
79 7
            $this->respondAsJSON($data);
80
        }
81
82 9
        return true;
83
    }
84
85
    /**
86
     * @param $response
87
     */
88 1
    private function respondToSlashCommand($response)
89
    {
90
        /** @noinspection PhpUndefinedClassInspection */
91 1
        $request = new Request(
92 1
            'POST',
93 1
            $this->getSlackbot()->getRequest('response_url'),
94 1
            ['Content-Type' => 'application/json'],
95
            json_encode([
96 1
                'text'          => $response,
97 1
                'response_type' => 'in_channel',
98
            ])
99
        );
100
101
        /* @noinspection PhpUndefinedClassInspection */
102 1
        $this->getClient()->send($request);
103 1
    }
104
105
    /**
106
     * @param $data
107
     */
108 1
    private function respondToSlack($data)
109
    {
110 1
        $this->getApiClient()->chatPostMessage($data);
111 1
    }
112
113
    /**
114
     * @param $data
115
     */
116 7
    private function respondAsJSON($data)
117
    {
118
        // headers_sent is used to avoid issue in the test
119 7
        if (!headers_sent()) {
120 7
            header('Content-type:application/json;charset=utf-8');
121
        }
122
123 7
        echo json_encode($data);
124 7
    }
125
126
    /**
127
     * Specify the response type
128
     * If response in config is set to empty, it will be considered based on listener.
129
     *
130
     * @return mixed|string
131
     */
132 9
    private function getResponseType()
133
    {
134 9
        if ($this->getSlackbot()->getRequest('debug') === true
135 9
            || $this->getSlackbot()->getRequest('debug') === 'true') {
136 7
            return 'json';
137
        }
138
139
        // response type in the config is empty, so choose it based on listener type
140 2
        return $this->getResponseByListenerType();
141
    }
142
143
    /**
144
     * @return string|null
145
     */
146 2
    private function getResponseByListenerType()
147
    {
148 2
        $listener = $this->getConfig()->get('listener');
149
        switch ($listener) {
150 2
            case 'slashCommand':
151 1
                return 'slashCommand';
152 1
            case 'event':
153 1
                return 'slack';
154
        }
155
    }
156
157
    /**
158
     * Send confirmation.
159
     */
160 1
    public function sendConfirmation()
161
    {
162 1
        $userId = $this->getSlackbot()->getRequest('user_id');
163
164 1
        $user = '';
165 1
        if (!empty($userId)) {
166 1
            $user = $this->getMessageUtility()->linkToUser($userId).' ';
167
        }
168
169 1
        $confirmMessage = $this->getConfig()->get('confirmReceivedMessage', ['user' => $user]);
170
171 1
        if (!empty($confirmMessage)) {
172 1
            $this->send($confirmMessage);
173
        }
174 1
    }
175
}
176