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

SlashCommandListener   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.67%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extractRequest() 0 11 2
B verifyOrigin() 0 29 4
A isThisBot() 0 7 3
A getChannelId() 0 4 1
A getKey() 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
                '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