Passed
Branch develop (0ae1ad)
by Kevin
02:45
created

ContextList::writeNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 6
1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\Config;
6
use Magium\Configuration\MagiumConfigurationFactory;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ContextList extends Command
13
{
14
    const TAB = '    ';
15
    const COMMAND = 'magium:configuration:list-contexts';
16
17
    protected function configure()
18
    {
19
        $this
20
            ->setName(self::COMMAND)
21
            ->setDescription('List contexts')
22
            ->setHelp("This command lists the context hierarchy")
23
        ;
24
    }
25
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $output->writeln('Context List');
29
        $output->writeln(['Following is list of contexts shown by inheritance', '']);
30
        $factory = new MagiumConfigurationFactory();
31
        $contextFile = $factory->getContextFile();
32
        $context = $contextFile->toXml();
33
        $output->writeln($this->formatNode('default', 'Default'));
34
        foreach ($context->children() as $child) {
35
            $this->writeNode($child, $output);
36
        }
37
    }
38
39
    protected function formatNode($id, $name = null)
40
    {
41
        if ($name == null) {
42
            $name = $id;
43
        }
44
        return sprintf('%s (%s)', $id, $name);
45
    }
46
47
    protected function writeNode(\SimpleXMLElement $element, OutputInterface $output, $tab = self::TAB)
48
    {
49
        $id = (string)$element['id'];
50
        $title = (string)$element['title'];
51
        $output->writeln($tab . $this->formatNode($id, $title));
52
        foreach ($element->children() as $child) {
53
            $this->writeNode($child, $output, $tab . self::TAB);
54
        }
55
    }
56
57
}
58