ContextRepository   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 72
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getContextHierarchy() 0 16 2
A getContextList() 0 18 3
A getContextFile() 0 4 1
A buildContextArray() 0 17 3
1
<?php
2
3
namespace Magium\Configuration\View\Controllers\Helpers;
4
5
use Magium\Configuration\Config\Repository\ConfigInterface;
6
use Magium\Configuration\File\Context\AbstractContextConfigurationFile;
7
8
class ContextRepository
9
{
10
11
    protected $contextFile;
12
13 11
    public function __construct(
14
        AbstractContextConfigurationFile $contextFile
15
    )
16
    {
17 11
        $this->contextFile = $contextFile;
18 11
    }
19
20 10
    public function getContextHierarchy()
21
    {
22 10
        $contextsFile = $this->getContextFile();
23 10
        $xml = $contextsFile->toXml();
24 10
        $children = [];
25 10
        foreach ($xml->children() as $child) {
26 1
            $children[] = $this->buildContextArray($child);
27
        }
28
        $contexts = [[
29 10
            'id' => 'default',
30 10
            'label' => 'Default',
31 10
            'children' => $children
32
        ]];
33
34 10
        return $contexts;
35
    }
36
37 1
    public function getContextList()
38
    {
39
        $contexts = [
40 1
            ConfigInterface::CONTEXT_DEFAULT   => 'Default'
41
        ];
42 1
        $xml = $this->contextFile->toXml();
43 1
        $xml->registerXPathNamespace('s', 'http://www.magiumlib.com/ConfigurationContext');
44 1
        $nodes = $this->contextFile->toXml()->xpath('//s:context');
45 1
        foreach ($nodes as $node) {
46 1
            $id = $label = (string)$node['id'];
47 1
            if (isset($node['label'])) {
48 1
                $label = (string)$node['label'];
49
            }
50 1
            $contexts[$id] = $label;
51
        }
52
53 1
        return $contexts;
54
    }
55
56 10
    public function getContextFile()
57
    {
58 10
        return $this->contextFile;
59
    }
60
61 1
    private function buildContextArray(\SimpleXMLElement $element)
62
    {
63 1
        $children = [];
64 1
        foreach ($element->children() as $child) {
65 1
            $children[] = $this->buildContextArray($child);
66
        }
67 1
        $label = (string)$element['id'];
68 1
        if (isset($element['label'])) {
69 1
            $label = (string)$element['label'];
70
        }
71
        $contexts = [
72 1
            'id' => (string)$element['id'],
73 1
            'label' => $label,
74 1
            'children' => $children
75
        ];
76 1
        return $contexts;
77
    }
78
79
}
80