HelpCommand::run()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 41
rs 8.6186
cc 7
nc 6
nop 2
1
<?php
2
namespace PortlandLabs\Slackbot\Command;
3
4
use League\CLImate\Argument\Filter;
5
use League\CLImate\Argument\Summary;
6
use PortlandLabs\Slackbot\Bot;
7
use PortlandLabs\Slackbot\Command\Argument\Manager as ArgumentManager;
8
use PortlandLabs\Slackbot\Permission\Checker;
9
use PortlandLabs\Slackbot\Slack\Rtm\Event\Message;
10
use Psr\Container\ContainerInterface;
11
12
class HelpCommand extends SimpleCommand
13
{
14
15
    protected $description = 'Get help';
16
17
    protected $signature = 'help {command? : The command to get help with}';
18
19
    protected $container;
20
21
    public function __construct(Bot $bot, ArgumentManager $argumentManager, Checker $checker, ContainerInterface $container)
22
    {
23
        $this->container = $container;
24
        parent::__construct($bot, $argumentManager, $checker);
25
    }
26
27
    /**
28
     * Handle a message
29
     *
30
     * @param Message $message
31
     * @param Manager $manager
32
     *
33
     * @return void
34
     */
35
    public function run(Message $message, ArgumentManager $manager)
36
    {
37
        $output = [];
38
39
        $filter = new Filter();
40
        $summary = new Summary();
41
42
        $userRole = $this->checker->getRole($message);
43
44
        $commandManager = $this->bot->commands();
45
        $commandName = $manager->get('command');
46
47
        $output[] = 'You can use the following commands:';
48
49
        foreach ($commandManager->all() as $command) {
50
            if (!$command instanceof ConsoleStyleCommand) {
51
                continue;
52
            }
53
54
            $arguments = new ArgumentManager();
55
            $arguments = $command->configure($arguments);
56
            $commandRole = $arguments->getRole();
57
58
            if (!$userRole instanceof $commandRole) {
59
                continue;
60
            }
61
62
            if ($commandName && $arguments->getCommand() === $commandName) {
63
                $this->outputUsage($message, $arguments, $command);
64
                return;
65
            }
66
67
            $filter->setArguments($arguments->all());
68
69
            // Output the simple usage statement
70
            $orderedArguments = array_merge($filter->withPrefix(), $filter->withoutPrefix());
71
            $short = $summary->short($orderedArguments);
72
            $output[] = "• `{$arguments->getCommand()}" . ($short ? " {$short}`" : '`') . ' ' . $command->getDescription();
73
        }
74
75
        $this->bot->feignTyping($message->getChannel(), implode("\n", $output));
0 ignored issues
show
Bug introduced by
It seems like $message->getChannel() can also be of type null; however, parameter $channel of PortlandLabs\Slackbot\Bot::feignTyping() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $this->bot->feignTyping(/** @scrutinizer ignore-type */ $message->getChannel(), implode("\n", $output));
Loading history...
76
    }
77
}