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 Illuminate\Http\Response as IlluminateResponse; |
9
|
|
|
use Illuminate\Routing\Controller as IlluminateController; |
10
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidHandler; |
11
|
|
|
use Spatie\SlashCommand\Exceptions\InvalidRequest; |
12
|
|
|
use Spatie\SlashCommand\Exceptions\SlackSlashCommandException; |
13
|
|
|
use Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled; |
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
|
|
|
$this->guardAgainstInvalidRequest(); |
33
|
|
|
|
34
|
|
|
$handler = $this->determineHandler(); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$response = $handler->handle($this->request); |
38
|
|
|
} catch (SlackSlashCommandException $exception) { |
39
|
|
|
$response = $exception->getResponse($this->request); |
40
|
|
|
} catch (Exception $exception) { |
41
|
|
|
$message = config('app.debug') ? (string) $exception : 'Whoops, something went wrong..'; |
42
|
|
|
|
43
|
|
|
$exception = new SlackSlashCommandException( |
44
|
|
|
$message, |
45
|
|
|
$exception->getCode(), |
46
|
|
|
$exception |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$response = $exception->getResponse($this->request); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $response->getIlluminateResponse(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function guardAgainstInvalidRequest() |
56
|
|
|
{ |
57
|
|
|
if (! request()->has('token')) { |
58
|
|
|
throw InvalidRequest::tokenNotFound(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->request->get('token') != $this->config->get('token')) { |
62
|
|
|
throw InvalidRequest::invalidToken($this->request->get('token')); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return \Spatie\SlashCommand\Handlers\BaseHandler |
68
|
|
|
* |
69
|
|
|
* @throws \Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled |
70
|
|
|
*/ |
71
|
|
|
protected function determineHandler() |
72
|
|
|
{ |
73
|
|
|
$handler = collect($this->config->get('handlers')) |
74
|
|
|
->map(function (string $handlerClassName) { |
75
|
|
|
if (! class_exists($handlerClassName)) { |
76
|
|
|
throw InvalidHandler::handlerDoesNotExist($handlerClassName); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return new $handlerClassName($this->request); |
80
|
|
|
}) |
81
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
82
|
|
|
return $handler->canHandle($this->request); |
|
|
|
|
83
|
|
|
}) |
84
|
|
|
->first(); |
85
|
|
|
|
86
|
|
|
if (! $handler) { |
87
|
|
|
throw RequestCouldNotBeHandled::noHandlerFound($this->request); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $handler; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: