1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
7
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
8
|
|
|
use Illuminate\Routing\Controller as IlluminateController; |
9
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
10
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidRequest; |
11
|
|
|
use Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled; |
12
|
|
|
|
13
|
|
|
class Controller extends IlluminateController |
14
|
|
|
{ |
15
|
|
|
/** @var \Spatie\SlashCommand\Request */ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** @var \Illuminate\Support\Collection */ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
public function __construct(IlluminateRequest $request, Repository $config) |
22
|
|
|
{ |
23
|
|
|
$this->request = Request::createFromIlluminateRequest($request); |
24
|
|
|
|
25
|
|
|
$this->config = collect($config->get('laravel-slack-slash-command')); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getResponse(): IlluminateResponse |
29
|
|
|
{ |
30
|
|
|
$this->guardAgainstInvalidRequest(); |
31
|
|
|
|
32
|
|
|
$handler = $this->determineHandler(); |
33
|
|
|
|
34
|
|
|
$response = $handler->handle($this->request); |
35
|
|
|
|
36
|
|
|
return $response->getIlluminateResponse(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function guardAgainstInvalidRequest() |
40
|
|
|
{ |
41
|
|
|
if (!request()->has('token')) { |
42
|
|
|
throw InvalidRequest::tokenNotFound(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->request->get('token') != $this->config->get('verification_token')) { |
46
|
|
|
throw InvalidRequest::invalidToken($this->request->get('token')); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Spatie\SlashCommand\Handlers\BaseHandler |
52
|
|
|
* |
53
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled |
54
|
|
|
*/ |
55
|
|
|
protected function determineHandler() |
56
|
|
|
{ |
57
|
|
|
$handler = collect($this->config->get('handlers')) |
58
|
|
|
->map(function (string $handlerClassName) { |
59
|
|
|
|
60
|
|
|
if (! class_exists($handlerClassName)) { |
61
|
|
|
throw InvalidHandler::handlerDoesNotExist($handlerClassName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new $handlerClassName($this->request); |
65
|
|
|
}) |
66
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
67
|
|
|
return $handler->canHandle($this->request); |
|
|
|
|
68
|
|
|
}) |
69
|
|
|
->first(); |
70
|
|
|
|
71
|
|
|
if (!$handler) { |
72
|
|
|
throw RequestCouldNotBeHandled::noHandlerFound($this->request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $handler; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: