Passed
Push — add-with-all-to-modules-list ( 82b81d...0edb38 )
by Chema
03:40
created

ListModulesCommand::generateListOfModules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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