Passed
Pull Request — develop (#35)
by Kevin
03:01
created

ContextList::formatNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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