Completed
Push — master ( 36581b...e31d04 )
by Ehsan
02:47
created

SlashCommandListener::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 2
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
                'success' => false,
41 2
                'message' => 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
                'success' => true,
54
                'message' => 'Awesome!',
55
            ];
56
        }
57
58
        return [
59 1
            'success' => false,
60
            'message' => '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
    /**
86
     * @return string
87
     */
88
    public function getKey(): string
89
    {
90
        return self::KEY;
91
    }
92
}
93