1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SlashCommand\Handlers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Spatie\SlashCommand\Attachment; |
8
|
|
|
use Spatie\SlashCommand\AttachmentField; |
9
|
|
|
use Spatie\SlashCommand\HandlesSlashCommand; |
10
|
|
|
use Spatie\SlashCommand\Request; |
11
|
|
|
use Spatie\SlashCommand\Response; |
12
|
|
|
|
13
|
|
|
class CatchAll extends BaseHandler |
14
|
|
|
{ |
15
|
|
|
protected $helpAvailable = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* If this function returns true, the handle method will get called. |
19
|
|
|
* |
20
|
|
|
* @param \Spatie\SlashCommand\Request $request |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function canHandle(Request $request): bool |
25
|
|
|
{ |
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handle the given request. Remember that Slack expects a response |
31
|
|
|
* within three seconds after the slash command was issued. If |
32
|
|
|
* there is more time needed, dispatch a job. |
33
|
|
|
* |
34
|
|
|
* @param \Spatie\SlashCommand\Request $request |
35
|
|
|
* |
36
|
|
|
* @return \Spatie\SlashCommand\Response |
37
|
|
|
*/ |
38
|
|
|
public function handle(Request $request): Response |
39
|
|
|
{ |
40
|
|
|
$response = $this->respondToSlack("I did not recognize this command: `/{$request->command} {$request->text}`"); |
41
|
|
|
|
42
|
|
|
list($command) = explode(' ' , $this->request->text); |
43
|
|
|
|
44
|
|
|
$alternatives = $this->findAlternatives($command); |
45
|
|
|
if (! $alternatives->isEmpty()) { |
46
|
|
|
$response->withAttachment($this->getCommandListAttachment($alternatives)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($this->helpAvailable) { |
50
|
|
|
$response->withAttachment(Attachment::create() |
51
|
|
|
->setText("For all available commands, try `/{$request->command} help`") |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function findAlternatives(string $command): Collection |
58
|
|
|
{ |
59
|
|
|
// Number of characters to change |
60
|
|
|
$threshold = 2; |
61
|
|
|
|
62
|
|
|
$handlers = collect(config('laravel-slack-slash-command.handlers')) |
63
|
|
|
->map(function (string $handlerClassName) { |
64
|
|
|
return new $handlerClassName($this->request); |
65
|
|
|
}) |
66
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
67
|
|
|
return $handler instanceof SignatureHandler; |
68
|
|
|
}) |
69
|
|
|
->filter(function (SignatureHandler $handler) { |
70
|
|
|
$signatureParts = new SignatureParts($handler->getSignature()); |
|
|
|
|
71
|
|
|
return Str::is($signatureParts->getSlashCommandName(), $this->request->command); |
72
|
|
|
}) |
73
|
|
|
->map(function(SignatureHandler $handler){ |
74
|
|
|
if ($handler instanceof Help) { |
|
|
|
|
75
|
|
|
$this->helpAvailable = true; |
76
|
|
|
} |
77
|
|
|
return $handler; |
78
|
|
|
}) |
79
|
|
|
; |
80
|
|
|
|
81
|
|
|
if (strpos($command, ':') !== false) { |
82
|
|
|
$subHandlers = $this->findInNamespace($handlers, $command); |
83
|
|
|
if (! $subHandlers->isEmpty()) { |
84
|
|
|
return $subHandlers; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $handlers->filter(function(SignatureHandler $handler) use($command, $threshold) { |
89
|
|
|
return levenshtein($handler->getName(), $command) <= $threshold; |
90
|
|
|
}); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function findInNamespace(Collection $handlers, string $command): Collection |
94
|
|
|
{ |
95
|
|
|
// Find commands in the same namespace |
96
|
|
|
list($namespace, $subCommand) = explode(':', $command); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$subHandlers = $handlers->filter(function (SignatureHandler $handler) use($namespace) { |
99
|
|
|
return Str::startsWith($handler->getName(), $namespace . ':' ); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
return $subHandlers; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function getCommandListAttachment(Collection $handlers): Attachment |
106
|
|
|
{ |
107
|
|
|
$attachmentFields = $handlers->map(function (SignatureHandler $handler) { |
108
|
|
|
return AttachmentField::create($handler->getFullCommand(), $handler->getDescription()); |
|
|
|
|
109
|
|
|
}) |
110
|
|
|
->all(); |
111
|
|
|
|
112
|
|
|
return Attachment::create() |
113
|
|
|
->setTitle('Did you mean:') |
114
|
|
|
->setFields($attachmentFields); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.