Completed
Pull Request — master (#50)
by Robbie
05:48
created

DumpCommand::getParsedOutput()   D

Complexity

Conditions 10
Paths 6

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 4.8196
cc 10
eloc 21
nc 6
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverLeague\Console\Command\Config;
4
5
use SilverLeague\Console\Command\Config\AbstractConfigCommand;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Outputs a combined representation of all SilverStripe configuration
13
 *
14
 * @package silverstripe-console
15
 * @author  Robbie Averill <[email protected]>
16
 */
17
class DumpCommand extends AbstractConfigCommand
18
{
19
    /**
20
     * @var string|null
21
     */
22
    protected $filter;
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('config:dump')
31
            ->setDescription('Dumps all of the processed configuration properties and their values')
32
            ->addOption('filter', null, InputOption::VALUE_REQUIRED, 'Filter the results (search)');
33
34
        $this->setHelp(<<<HELP
35
Dumps all of the processed configuration properties and their values. You can optionally add the --filter option
36
with a search value to narrow the results.
37
HELP
38
        );
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @throws InvalidArgumentException If an invalid configuration type is provided
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->filter = $input->getOption('filter');
50
51
        $data = $this->getParsedOutput($this->getConfig()->getAll());
52
        if ($this->filter) {
53
            $data = $this->filterOutput($data, $this->filter);
54
        }
55
56
        $table = new Table($output);
57
        $table
58
            ->setHeaders(['Class name', 'Property', 'Key', 'Value'])
59
            ->setRows($data)
60
            ->render();
61
    }
62
63
    /**
64
     * Creates a table-friendly output array from the input configuration source
65
     *
66
     * @param  array $data
67
     * @return array
68
     */
69
    protected function getParsedOutput($data)
70
    {
71
        $output = [];
72
        foreach ($data as $className => $classInfo) {
73
            foreach ($classInfo as $property => $values) {
74
                $row = [$className, $property];
75
                if (is_array($values)) {
76
                    foreach ($values as $key => $value) {
77
                        $row[] = is_numeric($key) ? '' : $key;
78
                        $row[] = is_array($value) ? json_encode($value, JSON_PRETTY_PRINT) : $value;
79
                        if (array_filter($row) != []) {
80
                            $output[] = $row;
81
                        }
82
                        // We need the class and property data for second level values if we're filtering.
83
                        if ($this->filter !== null) {
84
                            $row = [$className, $property];
85
                        } else {
86
                            $row = ['', ''];
87
                        }
88
                    }
89
                } else {
90
                    $row[] = '';
91
                    $row[] = $values;
92
                }
93
                if (array_filter($row) != []) {
94
                    $output[] = $row;
95
                }
96
            }
97
        }
98
        return $output;
99
    }
100
101
    /**
102
     * Apply a search filter to the results
103
     *
104
     * @param  array  $data   The pre-parsed output data
105
     * @param  string $filter The string to search on
106
     * @return array          Rows that have a string match on any of their fields
107
     */
108
    protected function filterOutput($data, $filter)
109
    {
110
        $output = [];
111
        foreach ($data as $values) {
112
            foreach ($values as $value) {
113
                if (is_string($value) && stripos($value, $filter) !== false) {
114
                    $output[] = $values;
115
                }
116
            }
117
        }
118
        return $output;
119
    }
120
}
121