Completed
Push — develop ( 9a303b...b58bed )
by Kevin
06:57 queued 04:05
created

View::getType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Magium\Configuration\View\Controllers;
4
5
use Interop\Container\ContainerInterface;
6
use Magium\Configuration\Config\BuilderInterface;
7
use Magium\Configuration\Config\MergedStructure;
8
use Magium\Configuration\Config\Repository\ConfigurationRepository;
9
use Magium\Configuration\Config\Storage\StorageInterface;
10
use Magium\Configuration\Source\SourceInterface;
11
use Magium\Configuration\View\UnableToCreateInstanceException;
12
use Magium\Configuration\View\ViewConfiguration;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Zend\View\Model\ViewModel;
15
16
class View implements ControllerInterface
17
{
18
19
    protected $viewConfiguration;
20
    protected $builder;
21
    protected $mergedConfiguration;
22
    protected $storage;
23
    protected $container;
24
    protected $context;
25
26 8
    public function __construct(
27
        ViewConfiguration $viewConfiguration,
28
        BuilderInterface $builder,
29
        MergedStructure $mergedConfiguration,
30
        StorageInterface $storage,
31
        $context,
32
        ContainerInterface $container = null
33
    )
34
    {
35 8
        $this->viewConfiguration = $viewConfiguration;
36 8
        $this->mergedConfiguration = $mergedConfiguration;
37 8
        $this->builder = $builder;
38 8
        $this->container = $container;
39 8
        $this->context = $context;
40 8
        $this->storage = $storage;
41 8
    }
42
43 1
    public function execute(ServerRequestInterface $request)
44
    {
45 1
        $params = $request->getQueryParams();
46 1
        if (!isset($params['context'])) {
47 1
            $params['context'] = ConfigurationRepository::CONTEXT_DEFAULT;
48
        }
49 1
        $groups = $this->buildSectionArray($params['section']);
50 1
        $viewModel = new ViewModel([
51 1
            'groups' => $groups,
52 1
            'section' => $params['section']
53
        ]);
54 1
        $viewModel->setTemplate($this->viewConfiguration->getViewFile());
55 1
        return $viewModel;
56
    }
57
58
    /**
59
     * @param $class
60
     * @return SourceInterface
61
     * @throws UnableToCreateInstanceException
62
     */
63
64 4
    public function getSource($class)
65
    {
66 4
        if ($this->container instanceof ContainerInterface) {
67 1
            if ($this->container->has($class)) {
68 1
                return $this->container->get($class);
69
            }
70
        }
71 3
        $reflectionClass = new \ReflectionClass($class);
72 3
        if (!$reflectionClass->implementsInterface(SourceInterface::class)) {
73 1
            throw new UnableToCreateInstanceException('Source model must implement ' . SourceInterface::class);
74
        }
75 2
        if (!$reflectionClass->getConstructor() || count($reflectionClass->getConstructor()->getParameters()) == 0) {
76 1
            return $reflectionClass->newInstance();
77
        }
78 1
        throw new UnableToCreateInstanceException('If a source model requires constructor parameters a service manager or DI container must be provided');
79
    }
80
81 6
    public function getStorage()
82
    {
83 6
        return $this->storage;
84
    }
85
86 6
    public function getContext()
87
    {
88 6
        return $this->context;
89
    }
90
91 8
    public function buildSectionArray($section)
92
    {
93 8
        $xpath = sprintf('//s:section[@identifier="%s" and not(@hidden="yes")]/s:group[not(@hidden="yes")]/s:element[not(@hidden="yes")]', $section);
94 8
        $this->mergedConfiguration->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
95 8
        $nodes = $this->mergedConfiguration->xpath($xpath);
96 8
        $groups = [];
97 8
        foreach ($nodes as $node) {
98
            /* @var $node \SimpleXMLElement */
99 8
            $group = $node->xpath('..')[0];
100 8
            $groupIdentifier = $name = (string)$group['identifier'];
101 8
            if (!isset($groups[$groupIdentifier])) {
102 8
                if (isset($group['label'])) {
103 3
                    $name = (string)$group['label'];
104
                }
105 8
                $groups[$groupIdentifier] = [
106 8
                    'label' => $name,
107
                    'children' => []
108
                ];
109
            }
110 8
            $identifier = $label = (string)$node['identifier'];
0 ignored issues
show
Unused Code introduced by
$label is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111 8
            $permittedValues = $this->getPermittedValues($node);
112 8
            $source = $this->getSourceData($node);
113 6
            $description = $this->getDescription($node);
114 6
            $label = $this->getLabel($node);
115 6
            $type = $this->getType($node);
116
117 6
            $path = $this->generatePath($node);
118 6
            $value = $this->getStorage()->getValue($path, $this->getContext());
119
120 6
            $groups[$groupIdentifier]['children'][$identifier] = [
121 6
                'path' => $path,
122 6
                'value' => $value,
123 6
                'source' => $source,
124 6
                'permittedValues' => $permittedValues,
125 6
                'type' => $type,
126 6
                'description' => $description,
127 6
                'label' => $label
128
            ];
129
        }
130 6
        return $groups;
131
    }
132
133 8
    protected function getSourceData(\SimpleXMLElement $node)
134
    {
135 8
        if (isset($node['source'])) {
136 4
            return $this->getSource((string)$node['source'])->getSourceData();
137 4
        } else if (isset($node->permittedValues)) {
138 1
            $source = [];
139 1
            foreach ($node->permittedValues->children() as $value) {
140 1
                $source[] = (string)$value;
141
            }
142 1
            return $source;
143
        }
144 4
        return [];
145
    }
146
147 6
    protected function getLabel(\SimpleXMLElement $node)
148
    {
149 6
        if (isset($node['label'])) {
150 3
            return (string)$node['label'];
151
        }
152 3
        return (string)$node['identifier'];
153
    }
154
155 6
    protected function getType(\SimpleXMLElement $node)
156
    {
157 6
        $type = 'text';
158 6
        if (isset($node['type'])) {
159 1
            $type = (string)$node['type'];
160
        }
161 6
        return $type;
162
    }
163
164 6
    protected function getDescription(\SimpleXMLElement $node)
165
    {
166 6
        if (isset($node->description)) {
167 2
            return (string)$node->description;
168
        }
169 5
        return '';
170
    }
171
172 8
    protected function getPermittedValues(\SimpleXMLElement $node)
173
    {
174 8
        $permittedValues = [];
175 8
        if (isset($node->permittedValues)) {
176 1
            foreach ($node->permittedValues->value as $value) {
177 1
                $permittedValues[] = (string)$value;
178
            }
179
        }
180 8
        return $permittedValues;
181
    }
182
183 6
    protected function generatePath(\SimpleXMLElement $node)
184
    {
185 6
        $element = (string)$node['identifier'];
186
187 6
        $node = $node->xpath('..')[0];
188 6
        $group = (string)$node['identifier'];
189
190 6
        $node = $node->xpath('..')[0];
191 6
        $section = (string)$node['identifier'];
192
193 6
        return sprintf('%s/%s/%s', $section, $group, $element);
194
    }
195
}
196