Completed
Pull Request — master (#104)
by Nicolas
03:17
created

Display::displayValues()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma\Console;
6
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Karma\Configuration;
11
use Karma\Command;
12
use Karma\Configuration\ValueFilterIterator;
13
14
class Display extends Command
15
{
16
    const
17
        ENV_DEV = 'dev',
18
        NO_FILTERING = 'karma-nofiltering';
19
20 36
    protected function configure(): void
21
    {
22 36
        parent::configure();
23
24
        $this
25 36
            ->setName('display')
26 36
            ->setDescription('Display environment variable set')
27
28 36
            ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_DEV)
29 36
            ->addOption('value', 'f', InputOption::VALUE_REQUIRED, 'Display only variable with this value', self::NO_FILTERING)
30
        ;
31 36
    }
32
33 4
    protected function execute(InputInterface $input, OutputInterface $output): int
34
    {
35 4
        parent::execute($input, $output);
36
37 4
        $environment = $input->getOption('env');
38
39 4
        $this->output->writeln(sprintf(
40 4
            "<info>Display <comment>%s</comment> values</info>\n",
41
            $environment
42
        ));
43
44 4
        $reader = $this->app['configuration'];
45 4
        $reader->setDefaultEnvironment($input->getOption('env'));
46
47 4
        $this->displayValues($reader, $input->getOption('value'));
48
49 4
        return 0;
50
    }
51
52 4
    private function displayValues(Configuration $reader, $filter = self::NO_FILTERING): void
53
    {
54 4
        $values = new \ArrayIterator($reader->getAllValuesForEnvironment());
55
56 4
        if($filter !== self::NO_FILTERING)
57
        {
58 1
            $values = new ValueFilterIterator($filter, $values);
59
        }
60
61 4
        $this->output->writeln('');
62
63 4
        $values->ksort();
64
65 4
        foreach($values as $variable => $value)
66
        {
67 4
            $this->output->writeln(sprintf(
68 4
               '<fg=cyan>%s</fg=cyan> = %s',
69
                $variable,
70 4
                $this->formatValue($value)
71
            ));
72
        }
73 4
    }
74
}
75