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

SlashCommandListener::extractRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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