SlashCommandListener   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 78
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extractRequest() 0 11 2
A verifyOrigin() 0 29 4
A isThisBot() 0 7 3
A getChannelId() 0 4 1
1
<?php
2
3
namespace Botonomous\listener;
4
5
use Botonomous\BotonomousException;
6
7
class SlashCommandListener extends AbstractBaseListener
8
{
9
    const KEY = 'slashCommand';
10
    const VERIFICATION_TOKEN = 'verificationToken';
11
    const MISSING_TOKEN_MESSAGE = 'Token is missing';
12
    const MISSING_TOKEN_CONFIG_MESSAGE = 'Token must be set in the config';
13
14
    /**
15
     * @return mixed
16
     */
17 9
    public function extractRequest()
18
    {
19 9
        $postRequest = $this->getRequestUtility()->getPost();
20
21 9
        if (empty($postRequest)) {
22
            /* @noinspection PhpInconsistentReturnPointsInspection */
23 5
            return;
24
        }
25
26 4
        return $postRequest;
27
    }
28
29
    /**
30
     * @throws \Exception
31
     *
32
     * @return array
33
     */
34 6
    public function verifyOrigin(): array
35
    {
36 6
        $token = $this->getRequest('token');
37
38 6
        if (empty($token)) {
39
            return [
40 2
                parent::ORIGIN_VERIFICATION_SUCCESS_KEY => false,
41 2
                parent::ORIGIN_VERIFICATION_MESSAGE_KEY => self::MISSING_TOKEN_MESSAGE,
42
            ];
43
        }
44
45 5
        $expectedToken = $this->getConfig()->get(self::VERIFICATION_TOKEN);
46
47 5
        if (empty($expectedToken)) {
48 1
            throw new BotonomousException(self::MISSING_TOKEN_CONFIG_MESSAGE);
49
        }
50
51 5
        if ($token === $expectedToken) {
52
            return [
53 5
                parent::ORIGIN_VERIFICATION_SUCCESS_KEY => true,
54 5
                parent::ORIGIN_VERIFICATION_MESSAGE_KEY => 'Awesome!',
55
            ];
56
        }
57
58
        return [
59 1
            parent::ORIGIN_VERIFICATION_SUCCESS_KEY => false,
60 1
            parent::ORIGIN_VERIFICATION_MESSAGE_KEY => 'Token is not valid',
61
        ];
62
    }
63
64
    /**
65
     * @throws \Exception
66
     *
67
     * @return bool
68
     */
69 11
    public function isThisBot(): bool
70
    {
71 11
        $userId = $this->getRequest('user_id');
72 11
        $username = $this->getRequest('user_name');
73
74 11
        return ($userId == 'USLACKBOT' || $username == 'slackbot') ? true : false;
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function getChannelId(): string
81
    {
82 3
        return $this->getRequest('channel_id');
83
    }
84
}
85