ListConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Console\Command;
10
11
use Daikon\Config\ConfigProviderInterface;
12
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Command\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Question\ChoiceQuestion;
17
18
final class ListConfig extends Command
19
{
20
    private ConfigProviderInterface $configProvider;
21
22
    public function __construct(ConfigProviderInterface $configProvider)
23
    {
24
        parent::__construct();
25
26
        $this->configProvider = $configProvider;
27
    }
28
29
    protected function configure(): void
30
    {
31
        $this
32
            ->setName('config:ls')
33
            ->setDescription('List configuration settings.')
34
            ->addArgument(
35
                'path',
36
                InputArgument::OPTIONAL,
37
                'Filter values for a given path.'
38
            );
39
    }
40
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        if (!is_string($path = $input->getArgument('path'))) {
44
            //@todo improve root scope config listing
45
            $scopes = [
46
                'app',
47
                'connectors',
48
                'crates',
49
                'databases',
50
                'fixtures',
51
                'jobs',
52
                'migrations',
53
                'project',
54
                'secrets',
55
                'services',
56
            ];
57
            $path = $this->getHelper('question')->ask(
58
                $input,
59
                $output,
60
                new ChoiceQuestion('Available scopes:', $scopes)
61
            );
62
        }
63
64
        $configs = $this->configProvider->get($path, []);
65
        $this->renderValues($output, $configs);
66
67
        return 0;
68
    }
69
70
    private function renderValues(OutputInterface $output, array $settings, int $indent = 0): void
71
    {
72
        foreach ($settings as $key => $value) {
73
            if (is_scalar($value) || is_null($value)) {
74
                switch (true) {
75
                    case is_bool($value):
76
                        $value = $value === true ? 'true' : 'false';
77
                        break;
78
                    case is_null($value):
79
                        $value = 'null';
80
                        break;
81
                    case $value === '':
82
                        $value = '""';
83
                        break;
84
                }
85
                $output->writeln(str_repeat(' ', $indent*2)."<info>$key</info>: $value");
86
            } elseif (empty($value)) {
87
                $output->writeln(str_repeat(' ', $indent*2)."<info>$key</info>: []");
88
            } else {
89
                $output->writeln(str_repeat(' ', $indent*2)."<info>$key</info>: [");
90
                $this->renderValues($output, $value, ++$indent);
91
                $output->writeln(str_repeat(' ', --$indent*2)."]");
92
            }
93
        }
94
    }
95
}
96