Passed
Push — change-design-list-modules-sim... ( 9b3b88...851a43 )
by Jesús
03:05
created

ListModulesCommand::output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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