ListModulesCommand::generateSimpleView()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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