These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | /** |
||
16 | * If this function returns true, the handle method will get called. |
||
17 | * |
||
18 | * @param \Spatie\SlashCommand\Request $request |
||
19 | * |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function canHandle(Request $request): bool |
||
23 | { |
||
24 | return true; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Handle the given request. Remember that Slack expects a response |
||
29 | * within three seconds after the slash command was issued. If |
||
30 | * there is more time needed, dispatch a job. |
||
31 | * |
||
32 | * @param \Spatie\SlashCommand\Request $request |
||
33 | * |
||
34 | * @return \Spatie\SlashCommand\Response |
||
35 | */ |
||
36 | public function handle(Request $request): Response |
||
37 | { |
||
38 | $response = $this->respondToSlack("I did not recognize this command: `/{$request->command} {$request->text}`"); |
||
39 | |||
40 | list($command) = explode(' ', $this->request->text); |
||
41 | |||
42 | $alternativeHandlers = $this->findAlternativeHandlers($command); |
||
43 | |||
44 | if ($alternativeHandlers->count()) { |
||
45 | $response->withAttachment($this->getCommandListAttachment($alternativeHandlers)); |
||
46 | } |
||
47 | |||
48 | if ($this->containsHelpHandler($alternativeHandlers)) { |
||
49 | $response->withAttachment(Attachment::create() |
||
50 | ->setText("For all available commands, try `/{$request->command} help`") |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | return $response; |
||
55 | } |
||
56 | |||
57 | protected function findAlternativeHandlers(string $command): Collection |
||
58 | { |
||
59 | $alternativeHandlers = collect(config('laravel-slack-slash-command.handlers')) |
||
60 | ->map(function (string $handlerClassName) { |
||
61 | return new $handlerClassName($this->request); |
||
62 | }) |
||
63 | ->filter(function (HandlesSlashCommand $handler) { |
||
64 | return $handler instanceof SignatureHandler; |
||
65 | }) |
||
66 | View Code Duplication | ->filter(function (SignatureHandler $handler) { |
|
67 | $signatureParts = new SignatureParts($handler->getSignature()); |
||
68 | |||
69 | return Str::is($signatureParts->getSlashCommandName(), $this->request->command); |
||
70 | }); |
||
71 | |||
72 | if (strpos($command, ':') !== false) { |
||
73 | $subHandlers = $this->findInNamespace($alternativeHandlers, $command); |
||
74 | if ($subHandlers->count()) { |
||
75 | return $subHandlers; |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return $alternativeHandlers->filter(function (SignatureHandler $handler) use ($command) { |
||
80 | return levenshtein($handler->getName(), $command) <= 2; |
||
81 | }); |
||
82 | } |
||
83 | |||
84 | protected function findInNamespace(Collection $handlers, string $command): Collection |
||
85 | { |
||
86 | // Find commands in the same namespace |
||
87 | list($namespace, $subCommand) = explode(':', $command); |
||
88 | |||
89 | $subHandlers = $handlers->filter(function (SignatureHandler $handler) use ($namespace) { |
||
90 | return Str::startsWith($handler->getName(), $namespace.':'); |
||
91 | }); |
||
92 | |||
93 | return $subHandlers; |
||
94 | } |
||
95 | |||
96 | View Code Duplication | protected function getCommandListAttachment(Collection $handlers): Attachment |
|
0 ignored issues
–
show
|
|||
97 | { |
||
98 | $attachmentFields = $handlers |
||
99 | ->map(function (SignatureHandler $handler) { |
||
100 | return AttachmentField::create($handler->getFullCommand(), $handler->getDescription()); |
||
101 | }) |
||
102 | ->all(); |
||
103 | |||
104 | return Attachment::create() |
||
105 | ->setColor('warning') |
||
106 | ->setTitle('Did you mean:') |
||
107 | ->setFields($attachmentFields); |
||
108 | } |
||
109 | |||
110 | protected function containsHelpHandler(Collection $alternativeHandlers): bool |
||
111 | { |
||
112 | return ! $alternativeHandlers->filter(function (SignatureHandler $handler) { |
||
113 | return $handler instanceof Help; |
||
114 | })->isEmpty(); |
||
115 | } |
||
116 | } |
||
117 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.