1 | <?php |
||
10 | abstract class AbstractBaseListener |
||
11 | { |
||
12 | const ORIGIN_VERIFICATION_SUCCESS_KEY = 'success'; |
||
13 | const ORIGIN_VERIFICATION_MESSAGE_KEY = 'message'; |
||
14 | |||
15 | private $config; |
||
16 | private $request; |
||
17 | private $requestUtility; |
||
18 | |||
19 | /** |
||
20 | * listen. |
||
21 | * |
||
22 | * @throws \Exception |
||
23 | * |
||
24 | * @return mixed |
||
25 | */ |
||
26 | 10 | public function listen() |
|
27 | { |
||
28 | // This is needed otherwise timeout error is displayed |
||
29 | 10 | $this->respondOK(); |
|
30 | |||
31 | 10 | $request = $this->extractRequest(); |
|
32 | |||
33 | 10 | if (empty($request)) { |
|
34 | /* @noinspection PhpInconsistentReturnPointsInspection */ |
||
35 | 6 | return; |
|
36 | } |
||
37 | |||
38 | 4 | $this->setRequest($request); |
|
39 | |||
40 | 4 | if ($this->isThisBot() !== false) { |
|
41 | /* @noinspection PhpInconsistentReturnPointsInspection */ |
||
42 | 2 | return; |
|
43 | } |
||
44 | |||
45 | 2 | return $request; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * @return mixed |
||
50 | */ |
||
51 | abstract public function extractRequest(); |
||
52 | |||
53 | /** |
||
54 | * @return string |
||
55 | */ |
||
56 | abstract public function getChannelId(): string; |
||
57 | |||
58 | /** |
||
59 | * @param null|string $key |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 35 | public function getRequest(string $key = null) |
|
64 | { |
||
65 | 35 | if (!isset($this->request)) { |
|
66 | // each listener has its own way of extracting the request |
||
67 | 10 | $this->setRequest($this->extractRequest()); |
|
68 | } |
||
69 | |||
70 | 35 | if ($key === null) { |
|
71 | // return the entire request since key is null |
||
72 | 18 | return $this->request; |
|
73 | } |
||
74 | |||
75 | 25 | if (is_array($this->request) && array_key_exists($key, $this->request)) { |
|
76 | 23 | return $this->request[$key]; |
|
77 | } |
||
78 | 9 | } |
|
79 | |||
80 | /** |
||
81 | * @param array $request |
||
82 | */ |
||
83 | 45 | public function setRequest($request) |
|
84 | { |
||
85 | 45 | $this->request = $request; |
|
86 | 45 | } |
|
87 | |||
88 | /** |
||
89 | * @return Config |
||
90 | */ |
||
91 | 8 | public function getConfig(): Config |
|
92 | { |
||
93 | 8 | if (!isset($this->config)) { |
|
94 | 5 | $this->setConfig(new Config()); |
|
95 | } |
||
96 | |||
97 | 8 | return $this->config; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param Config $config |
||
102 | */ |
||
103 | 12 | public function setConfig(Config $config) |
|
104 | { |
||
105 | 12 | $this->config = $config; |
|
106 | 12 | } |
|
107 | |||
108 | /** |
||
109 | * Verify the request comes from Slack |
||
110 | * Each listener must have have this and has got its own way to check the request. |
||
111 | * |
||
112 | * @throws \Exception |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | abstract public function verifyOrigin(); |
||
117 | |||
118 | /** |
||
119 | * Check if the request belongs to the bot itself. |
||
120 | * |
||
121 | * @throws \Exception |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | abstract public function isThisBot(): bool; |
||
126 | |||
127 | /** |
||
128 | * @return RequestUtility |
||
129 | */ |
||
130 | 24 | public function getRequestUtility(): RequestUtility |
|
131 | { |
||
132 | 24 | if (!isset($this->requestUtility)) { |
|
133 | 8 | $this->setRequestUtility((new RequestUtility())); |
|
138 | |||
139 | /** |
||
140 | * @param RequestUtility $requestUtility |
||
141 | */ |
||
142 | 25 | public function setRequestUtility(RequestUtility $requestUtility) |
|
146 | |||
147 | /** |
||
148 | * respondOK. |
||
149 | */ |
||
150 | 10 | protected function respondOK() |
|
186 | |||
187 | /** |
||
188 | * @throws \Exception |
||
189 | * |
||
190 | * @return array<string,boolean|string> |
||
191 | */ |
||
192 | 5 | public function verifyRequest(): array |
|
215 | |||
216 | /** |
||
217 | * @return string|null |
||
218 | */ |
||
219 | 8 | public function determineAction() |
|
234 | |||
235 | /** |
||
236 | * Return message based on the listener |
||
237 | * If listener is event and event text is empty, fall back to request text. |
||
238 | * |
||
239 | * @throws \Exception |
||
240 | * |
||
241 | * @return mixed|string |
||
242 | */ |
||
243 | 9 | public function getMessage() |
|
255 | } |
||
256 |