Passed
Push — master ( ff83e3...5f4a5c )
by Ehsan
03:54
created

EventListener::setAppId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Botonomous\listener;
4
5
use Botonomous\Event;
6
use Botonomous\utility\StringUtility;
7
8
/**
9
 * Class EventListener.
10
 */
11
class EventListener extends AbstractBaseListener
12
{
13
    private $token;
14
    private $teamId;
15
    private $appId;
16
    private $event;
17
    private $requestEventMaps = [
18
        'ts'       => 'timestamp',
19
        'event_ts' => 'eventTimestamp',
20
    ];
21
22
    /**
23
     * @return mixed
24
     */
25 12
    public function extractRequest()
26
    {
27 12
        return $this->getRequestUtility()->getPostedBody();
28
    }
29
30
    /**
31
     * @return string
32
     */
33 1
    public function getToken()
34
    {
35 1
        return $this->token;
36
    }
37
38
    /**
39
     * @param string $token
40
     */
41 1
    public function setToken($token)
42
    {
43 1
        $this->token = $token;
44 1
    }
45
46
    /**
47
     * @return string
48
     */
49 1
    public function getTeamId()
50
    {
51 1
        return $this->teamId;
52
    }
53
54
    /**
55
     * @param string $teamId
56
     */
57 1
    public function setTeamId($teamId)
58
    {
59 1
        $this->teamId = $teamId;
60 1
    }
61
62
    /**
63
     * @return string
64
     */
65 1
    public function getAppId()
66
    {
67 1
        return $this->appId;
68
    }
69
70
    /**
71
     * @param string $appId
72
     */
73 1
    public function setAppId($appId)
74
    {
75 1
        $this->appId = $appId;
76 1
    }
77
78
    /**
79
     * @return Event
80
     */
81 12
    public function getEvent()
82
    {
83 12
        if (!isset($this->event)) {
84 11
            $this->loadEvent();
85
        }
86
87 11
        return $this->event;
88
    }
89
90
    /**
91
     * @param Event $event
92
     */
93 8
    public function setEvent(Event $event)
94
    {
95 8
        $this->event = $event;
96 8
    }
97
98
    /**
99
     * @throws \Exception
100
     */
101 11
    private function loadEvent()
102
    {
103 11
        $request = $this->getRequest();
104
105 11
        if (!isset($request['event'])) {
106 3
            return;
107
        }
108
109 8
        $request = $request['event'];
110
111 8
        if (!isset($request['type'])) {
112 1
            throw new \Exception('Event type must be specified');
113
        }
114
115
        // create the event
116 7
        $eventObject = new Event($request['type']);
117
118
        // exclude type from the args since it's already passed
119 7
        unset($request['type']);
120
121 7
        $stringUtility = new StringUtility();
122 7
        foreach ($request as $argKey => $argValue) {
123 7
            if (array_key_exists($argKey, $this->requestEventMaps)) {
124 6
                $argKey = $this->requestEventMaps[$argKey];
125
            }
126
127 7
            $setterName = 'set'.$stringUtility->snakeCaseToCamelCase($argKey);
128
129
            // ignore calling the method if setter does not exist
130 7
            if (!method_exists($eventObject, $setterName)) {
131 6
                continue;
132
            }
133
134 7
            $eventObject->$setterName($argValue);
135
        }
136
137
        // set it
138 7
        $this->setEvent($eventObject);
139 7
    }
140
141
    /**
142
     * @throws \Exception
143
     *
144
     * @return array<string,boolean|string>
145
     */
146 3
    public function verifyOrigin()
147
    {
148 3
        $request = $this->getRequest();
149
150 3
        if (!isset($request['token']) || !isset($request['api_app_id'])) {
151
            return [
152 1
                'success' => false,
153
                'message' => 'Token or api_app_id is not provided',
154
            ];
155
        }
156
157 3
        $verificationToken = $this->getConfig()->get('verificationToken');
158
159 3
        if (empty($verificationToken)) {
160 1
            throw new \Exception('Verification token must be provided');
161
        }
162
163 2
        $expectedAppId = $this->getConfig()->get('appId');
164
165 2
        if (empty($expectedAppId)) {
166 1
            throw new \Exception('Api app id must be provided');
167
        }
168
169 1
        if ($verificationToken === $request['token'] &&
170 1
            $expectedAppId === $request['api_app_id']) {
171
            return [
172 1
                'success' => true,
173
                'message' => 'O La la!',
174
            ];
175
        }
176
177
        return [
178 1
            'success' => false,
179
            'message' => 'Token or api_app_id mismatch',
180
        ];
181
    }
182
183
    /**
184
     * Check if the request belongs to the bot itself.
185
     *
186
     * @throws \Exception
187
     *
188
     * @return bool
189
     */
190 4
    public function isThisBot()
191
    {
192 4
        $subType = $this->getRequest('subtype');
193
194 4
        if ($subType === 'bot_message') {
195 1
            return true;
196
        }
197
198 4
        $event = $this->getEvent();
199
200 4
        return $event instanceof Event && !empty($event->getBotId());
201
    }
202
203
    /**
204
     * @return string
205
     */
206 1
    public function getChannelId()
207
    {
208 1
        return $this->getEvent()->getChannel();
209
    }
210
}
211