SlashCommandListener::verifyOrigin()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 13
cts 13
cp 1
rs 9.456
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
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