ListCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Style\StyleInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Zalas\Toolbox\Tool\Filter;
12
use Zalas\Toolbox\Tool\Tool;
13
use Zalas\Toolbox\UseCase\ListTools;
14
15
final class ListCommand extends Command
16
{
17
    use DefaultTag;
18
19
    public const NAME = 'list-tools';
20
21
    private ListTools $listTools;
22
23 9
    public function __construct(ListTools $listTools)
24
    {
25 9
        parent::__construct(self::NAME);
26
27 9
        $this->listTools = $listTools;
28
    }
29
30 9
    protected function configure(): void
31
    {
32 9
        $this->setDescription('Lists available tools');
33 9
        $this->addOption('exclude-tag', 'e', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to exclude', $this->defaultExcludeTag());
34 9
        $this->addOption('tag', 't', InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, 'Tool tags to filter by', $this->defaultTag());
35
    }
36
37 3
    protected function execute(InputInterface $input, OutputInterface $output): int
38
    {
39 3
        $tools = \call_user_func($this->listTools, new Filter($input->getOption('exclude-tag'), $input->getOption('tag')));
40
41 3
        $style = $this->createStyle($input, $output);
42 3
        $style->title('Available tools');
43 3
        $style->table(
44 3
            ['Name', 'Summary'],
45 3
            $tools->map(function (Tool $tool) {
46 3
                return [\sprintf('<info>%s</info>', $tool->name()), $tool->summary().PHP_EOL.$tool->website().PHP_EOL];
47 3
            })->toArray()
48 3
        );
49
50 3
        return 0;
51
    }
52
53 3
    private function createStyle(InputInterface $input, OutputInterface $output): StyleInterface
54
    {
55 3
        return new SymfonyStyle($input, $output);
56
    }
57
}
58