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 Help extends SignatureHandler |
14
|
|
|
{ |
15
|
|
|
protected $signature = '* help {command? : The command you want information about}'; |
16
|
|
|
|
17
|
|
|
protected $description = 'List all commands or provide information about all commands'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Handle the given request. |
21
|
|
|
* |
22
|
|
|
* @param \Spatie\SlashCommand\Request $request |
23
|
|
|
* |
24
|
|
|
* @return \Spatie\SlashCommand\Response |
25
|
|
|
*/ |
26
|
|
|
public function handle(Request $request): Response |
27
|
|
|
{ |
28
|
|
|
$handlers = $this->findAvailableHandlers(); |
29
|
|
|
|
30
|
|
|
if ($command = $this->getArgument('command')) { |
31
|
|
|
return $this->displayHelpForCommand($handlers, $command); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->displayHelpList($handlers); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Find all handlers that are available for the current SlashCommand |
39
|
|
|
* and have a signature |
40
|
|
|
* |
41
|
|
|
* @return Collection|SignatureHandler[] |
42
|
|
|
*/ |
43
|
|
|
protected function findAvailableHandlers(): Collection |
44
|
|
|
{ |
45
|
|
|
return collect(config('laravel-slack-slash-command.handlers')) |
46
|
|
|
->map(function (string $handlerClassName) { |
47
|
|
|
return new $handlerClassName($this->request); |
48
|
|
|
}) |
49
|
|
|
->filter(function (HandlesSlashCommand $handler) { |
50
|
|
|
return $handler instanceof SignatureHandler; |
51
|
|
|
}) |
52
|
|
|
->filter(function (SignatureHandler $handler) { |
53
|
|
|
$signatureParts = new SignatureParts($handler->getSignature()); |
54
|
|
|
return Str::is($signatureParts->getSlashCommandName(), $this->request->command); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Show the help information for a single SignatureHandler |
60
|
|
|
* |
61
|
|
|
* @param Collection|SignatureHandler[] $handlers |
62
|
|
|
* @param string $command |
63
|
|
|
* @return Response |
64
|
|
|
*/ |
65
|
|
|
protected function displayHelpForCommand(Collection $handlers, string $command): Response |
66
|
|
|
{ |
67
|
|
|
$helpRequest = clone $this->request; |
68
|
|
|
$helpRequest->text = $command; |
69
|
|
|
|
70
|
|
|
/** @var SignatureHandler $handler */ |
71
|
|
|
$handler = $handlers->filter(function (HandlesSlashCommand $handler) use ($helpRequest){ |
72
|
|
|
return $handler->canHandle($helpRequest); |
|
|
|
|
73
|
|
|
}) |
74
|
|
|
->first(); |
75
|
|
|
|
76
|
|
|
$field = AttachmentField::create($handler->getFullCommand(), $handler->getHelpDescription()); |
77
|
|
|
|
78
|
|
|
return $this->respondToSlack('') |
79
|
|
|
->withAttachment(Attachment::create() |
80
|
|
|
->addField($field) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Show a list of all available Handlers |
86
|
|
|
* |
87
|
|
|
* @param Collection|SignatureHandler[] $handlers |
88
|
|
|
* @return Response |
89
|
|
|
*/ |
90
|
|
|
protected function displayHelpList(Collection $handlers): Response |
91
|
|
|
{ |
92
|
|
|
$attachmentFields = $handlers->map(function (SignatureHandler $handler) { |
93
|
|
|
return AttachmentField::create($handler->getFullCommand(), $handler->getDescription()); |
94
|
|
|
}) |
95
|
|
|
->all(); |
96
|
|
|
|
97
|
|
|
return $this->respondToSlack("Available commands:") |
98
|
|
|
->withAttachment(Attachment::create() |
99
|
|
|
->setFields($attachmentFields) |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.