Passed
Pull Request — develop (#53)
by Kevin
08:43
created

ArrayConfigurationRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 48
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getValue() 0 4 1
A hasValue() 0 4 1
A getValueFlag() 0 5 1
A xpath() 0 16 4
1
<?php
2
3
namespace Magium\Configuration\Config\Repository;
4
5
use Magium\Configuration\Config\InvalidArgumentException;
6
7
class ArrayConfigurationRepository implements ConfigInterface
8
{
9
10
    protected $data;
11
12 10
    public function __construct($data)
13
    {
14 10
        if (!is_array($data)) {
15 1
            throw new InvalidArgumentException('Configuration data must be in an array format');
16
        }
17 9
        $this->data = $data;
18 9
    }
19
20 3
    public function getValue($path)
21
    {
22 3
        return $this->xpath($path);
23
    }
24
25
    public function hasValue($path)
26
    {
27
        return $this->xpath($path) !== null;
28
    }
29
30 2
    public function getValueFlag($path)
31
    {
32 2
        $value = $this->getValue($path);
33 2
        return in_array($value, self::ALLOWED_TRUES);
34
    }
35
36 9
    public function xpath($xpath)
37
    {
38 9
        $parts = explode('/', $xpath);
39 9
        if (count($parts) != 3) {
40 2
            throw new InvalidArgumentException('Path must be in the form of "section/group/element"');
41
        }
42 7
        $process = $this->data;
43 7
        foreach ($parts as $part) {
44 7
            if (isset($process[$part])) {
45 7
                $process = $process[$part];
46
            } else {
47 7
                return null;
48
            }
49
        }
50 6
        return $process;
51
    }
52
53
54
}
55