Passed
Push — analysis-wjv2QK ( 942ce9 )
by Arnaud
05:29 queued 10s
created

ShowConfig::printArray()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
nc 2
nop 2
dl 0
loc 21
rs 9.2222
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Command;
12
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputDefinition;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Shows the configuration.
20
 */
21
class ShowConfig extends Command
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('show:config')
30
            ->setDescription('Shows the configuration')
31
            ->setDefinition(
32
                new InputDefinition([
33
                    new InputArgument(
34
                        'path',
35
                        InputArgument::OPTIONAL,
36
                        'If specified, use the given path as working directory'
37
                    ),
38
                ])
39
            )
40
            ->setHelp('Shows the website\'s configuration');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $output->writeln('<info>Configuration:</info>');
49
50
        try {
51
            $output->writeln($this->printArray($this->getBuilder($output)->getConfig()->getAsArray()));
52
        } catch (\Exception $e) {
53
            throw new \Exception(sprintf($e->getMessage()));
54
        }
55
56
        return 0;
57
    }
58
59
    /**
60
     * Prints an array in console.
61
     *
62
     * @param array $array
63
     * @param int   $column
64
     *
65
     * @return string
66
     */
67
    private function printArray($array, $column = -2): string
68
    {
69
        $output = '';
70
71
        if (is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
72
            $column += 2;
73
            foreach ($array as $key => $val) {
74
                switch (gettype($val)) {
75
                    case 'array':
76
                        $output .= str_repeat(' ', $column)."$key:\n".$this->printArray($val, $column);
77
                        break;
78
                    case 'boolean':
79
                        $output .= str_repeat(' ', $column)."$key: ".($val ? 'true' : 'false')."\n";
80
                        break;
81
                    default:
82
                        $output .= str_repeat(' ', $column)."$key: $val\n";
83
                }
84
            }
85
        }
86
87
        return $output;
88
    }
89
}
90