|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Http\Response; |
|
7
|
|
|
use Spatie\SlashCommand\SlashCommandHandler\BaseHandler; |
|
8
|
|
|
|
|
9
|
|
|
class SlackCommandController |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var array */ |
|
12
|
|
|
protected $commandConfig; |
|
13
|
|
|
|
|
14
|
|
|
/** @var \Illuminate\Http\Request */ |
|
15
|
|
|
protected $request; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(array $commandConfig, Request $request) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$this->commandConfig = $commandConfig; |
|
20
|
|
|
|
|
21
|
|
|
$this->request = $request; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getResponse(): Response { |
|
25
|
|
|
|
|
26
|
|
|
$this->guardAgainstInvalidRequest(); |
|
27
|
|
|
|
|
28
|
|
|
$handler = $this->determineResponseHandler(); |
|
29
|
|
|
|
|
30
|
|
|
$response = $handler->handleCurrentRequest(); |
|
31
|
|
|
|
|
32
|
|
|
return $response->getHttpResponse(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function guardAgainstInvalidRequest() |
|
36
|
|
|
{ |
|
37
|
|
|
if (!request()->has('token')) { |
|
38
|
|
|
throw InvalidSlashCommandRequest::tokenNotFound(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (request()->get('token') != $this->commandConfig['verification_token']) { |
|
42
|
|
|
throw InvalidSlashCommandRequest::invalidToken(request()->get('token')); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function determineResponseHandler(): BaseHandler |
|
47
|
|
|
{ |
|
48
|
|
|
$handler = collect($this->commandConfig['handlers']) |
|
49
|
|
|
->map(function (string $handlerClassName) { |
|
50
|
|
|
return new $handlerClassName(request()); |
|
51
|
|
|
}) |
|
52
|
|
|
->filter(function (BaseHandler $handler) { |
|
53
|
|
|
return $handler->canHandleCurrentRequest(); |
|
54
|
|
|
})->first(); |
|
55
|
|
|
|
|
56
|
|
|
if (!$handler) { |
|
57
|
|
|
throw RequestCouldNotBeProcessed::noHandlerFound(request()); |
|
58
|
|
|
} |
|
59
|
|
|
return $handler; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|