ListModulesCommand::generateSimpleView()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 9
nop 1
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 5
rs 9.5222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Infrastructure\Command;
6
7
use Gacela\Console\ConsoleFacade;
8
use Gacela\Console\Domain\AllAppModules\AppModule;
9
use Gacela\Framework\DocBlockResolverAwareTrait;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\Table;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\ConsoleOutput;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * @method ConsoleFacade getFacade()
20
 */
21
final class ListModulesCommand extends Command
22
{
23
    use DocBlockResolverAwareTrait;
24
25
    private const CHECK_SYMBOL = '✔️';
26
27
    private const CROSS_SYMBOL = '✖️';
28
29
    private ?OutputInterface $output = null;
30 6
31
    protected function configure(): void
32 6
    {
33 6
        $this->setName('list:modules')
34 6
            ->setDescription('Render all modules found')
35 6
            ->addArgument('filter', InputArgument::OPTIONAL, 'Any filter to simplify the output')
36
            ->addOption('detailed', 'd', InputOption::VALUE_NONE, 'Display all the modules in detail');
37
    }
38 4
39
    protected function execute(InputInterface $input, OutputInterface $output): int
40 4
    {
41
        $this->output = $output;
42 4
43
        $filter = (string)$input->getArgument('filter');
44 4
45 4
        $this->generateListOfModules(
46 4
            (bool)$input->getOption('detailed'),
47 4
            $this->getFacade()->findAllAppModules($filter),
48
        );
49 4
50
        return self::SUCCESS;
51
    }
52 4
53
    private function output(): OutputInterface
54 4
    {
55
        return $this->output ?? new ConsoleOutput();
56
    }
57
58
    /**
59
     * @param list<AppModule> $modules
60 4
     */
61
    private function generateListOfModules(bool $isDetailed, array $modules): void
62 4
    {
63 1
        if ($isDetailed) {
64
            $this->generateDetailedView($modules);
65 3
        } else {
66
            $this->generateSimpleView($modules);
67
        }
68
    }
69
70
    /**
71
     * @param list<AppModule> $modules
72 1
     */
73
    private function generateDetailedView(array $modules): void
74 1
    {
75 1
        $result = '';
76 1
        foreach ($modules as $i => $module) {
77 1
            $n = $i + 1;
78 1
            $factory = $module->factoryClass() ?? self::CROSS_SYMBOL;
79 1
            $config = $module->configClass() ?? self::CROSS_SYMBOL;
80
            $provider = $module->providerClass() ?? self::CROSS_SYMBOL;
81 1
82 1
            $result .= <<<TXT
83 1
============================
84
{$n}.- <fg=green>{$module->moduleName()}</>
85 1
----------------------------
86 1
<fg=cyan>Facade</>: {$module->facadeClass()}
87 1
<fg=cyan>Factory</>: {$factory}
88 1
<fg=cyan>Config</>: {$config}
89
<fg=cyan>Provider</>: {$provider}
90 1
91
TXT;
92
        }
93 1
94
        $this->output()->write($result);
95
    }
96
97
    /**
98
     * @param list<AppModule> $modules
99 3
     */
100
    private function generateSimpleView(array $modules): void
101 3
    {
102
        $rows = [];
103 3
104 3
        foreach ($modules as $module) {
105 3
            $rows[] = [
106 3
                $module->fullModuleName(),
107 3
                self::CHECK_SYMBOL, // facade is always true
108 3
                $module->factoryClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
109 3
                $module->configClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
110 3
                $module->providerClass() !== null ? self::CHECK_SYMBOL : self::CROSS_SYMBOL,
111
            ];
112
        }
113 3
114 3
        $table = new Table($this->output());
115 3
        $table->setStyle('box');
116 3
        $table->setHeaders(['Module namespace', 'Facade', 'Factory', 'Config', 'Dep. Provider']);
117 3
        $table->setRows($rows);
118
        $table->render();
119
    }
120
}
121