Passed
Push — master ( b808b9...919917 )
by Ehsan
04:54
created

SlashCommandListener::verifyOrigin()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 10
cts 10
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Botonomous\listener;
4
5
class SlashCommandListener extends AbstractBaseListener
6
{
7
    const VERIFICATION_TOKEN = 'verificationToken';
8
9
    /**
10
     * @return mixed
11
     */
12 8
    public function extractRequest()
13
    {
14 8
        $postRequest = $this->getRequestUtility()->getPost();
15
16 8
        if (empty($postRequest)) {
17
            /* @noinspection PhpInconsistentReturnPointsInspection */
18 4
            return;
19
        }
20
21 4
        return $postRequest;
22
    }
23
24
    /**
25
     * @throws \Exception
26
     *
27
     * @return array
28
     */
29 5
    public function verifyOrigin()
30
    {
31 5
        $token = $this->getRequest('token');
32
33 5
        if (empty($token)) {
34
            return [
35 1
                'success' => false,
36
                'message' => 'Token is missing',
37
            ];
38
        }
39
40 5
        $expectedToken = $this->getConfig()->get(self::VERIFICATION_TOKEN);
41
42 5
        if (empty($expectedToken)) {
43 1
            throw new \Exception('Token must be set in the config');
44
        }
45
46 5
        if ($token === $expectedToken) {
47
            return [
48 5
                'success' => true,
49
                'message' => 'Awesome!',
50
            ];
51
        }
52
53
        return [
54 1
            'success' => false,
55
            'message' => 'Token is not valid',
56
        ];
57
    }
58
59
    /**
60
     * @throws \Exception
61
     *
62
     * @return bool
63
     */
64 11
    public function isThisBot()
65
    {
66 11
        $userId = $this->getRequest('user_id');
67 11
        $username = $this->getRequest('user_name');
68
69 11
        return ($userId == 'USLACKBOT' || $username == 'slackbot') ? true : false;
70
    }
71
}
72