1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Config\Repository; |
7
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
8
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
9
|
|
|
use Spatie\SlashCommand\Handlers\SignatureHandler; |
10
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
11
|
|
|
use Illuminate\Routing\Controller as IlluminateController; |
12
|
|
|
use Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled; |
13
|
|
|
use Spatie\SlashCommand\Exceptions\SlackSlashCommandException; |
14
|
|
|
|
15
|
|
|
class Controller extends IlluminateController |
16
|
|
|
{ |
17
|
|
|
/** @var \Spatie\SlashCommand\Request */ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** @var \Illuminate\Support\Collection */ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
public function __construct(IlluminateRequest $request, Repository $config) |
24
|
|
|
{ |
25
|
|
|
$this->request = Request::createFromIlluminateRequest($request); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$this->config = collect($config->get('laravel-slack-slash-command')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getResponse(): IlluminateResponse |
31
|
|
|
{ |
32
|
|
|
$handler = $this->determineHandler(); |
33
|
|
|
|
34
|
|
|
try { |
35
|
|
|
if ($handler instanceof SignatureHandler) { |
36
|
|
|
$handler->validate(); |
37
|
|
|
} |
38
|
|
|
$response = $handler->handle($this->request); |
39
|
|
|
} catch (SlackSlashCommandException $exception) { |
40
|
|
|
$response = $exception->getResponse($this->request); |
41
|
|
|
} catch (Exception $exception) { |
42
|
|
|
$response = $this->convertToResponse($exception); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $response->getIlluminateResponse(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return \Spatie\SlashCommand\Handlers\BaseHandler |
50
|
|
|
* |
51
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled |
52
|
|
|
*/ |
53
|
|
|
protected function determineHandler() |
54
|
|
|
{ |
55
|
|
|
$handler = collect($this->config->get('handlers')) |
56
|
|
|
->map(function (string $handlerClassName) { |
57
|
|
|
if (! class_exists($handlerClassName)) { |
58
|
|
|
throw InvalidHandler::handlerDoesNotExist($handlerClassName); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new $handlerClassName($this->request); |
62
|
|
|
}) |
63
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
64
|
|
|
return $handler->canHandle($this->request); |
|
|
|
|
65
|
|
|
}) |
66
|
|
|
->first(); |
67
|
|
|
|
68
|
|
|
if (! $handler) { |
69
|
|
|
throw RequestCouldNotBeHandled::noHandlerFound($this->request); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $handler; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function convertToResponse(Exception $exception) : Response |
76
|
|
|
{ |
77
|
|
|
$message = config('app.debug') ? (string) $exception : 'Whoops, something went wrong...'; |
78
|
|
|
|
79
|
|
|
$exception = new SlackSlashCommandException( |
80
|
|
|
$message, |
81
|
|
|
$exception->getCode(), |
82
|
|
|
$exception |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$response = $exception->getResponse($this->request); |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..