Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

ServiceAdapter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 92
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A processConfig() 0 10 4
A has() 0 4 1
A getData() 0 10 3
A getConfig() 0 8 2
A toArray() 0 4 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Configuration\ServiceAdapter\Base;
15
16
use InvalidArgumentException;
17
use WebHemi\Configuration\ServiceInterface;
18
19
/**
20
 * Class ServiceAdapter.
21
 */
22
class ServiceAdapter implements ServiceInterface
23
{
24
    /** @var array */
25
    private $pathMap = [];
26
    /** @var array */
27
    private $rawConfig;
28
29
    /**
30
     * ServiceAdapter constructor.
31
     *
32
     * @param array $config
33
     */
34 67
    public function __construct(array $config)
35
    {
36 67
        $this->rawConfig = $config;
37 67
        $this->processConfig('', $config);
38 67
    }
39
40
    /**
41
     * Processes the config into a one dimensional array.
42
     *
43
     * @param string $path
44
     * @param array  $config
45
     * @return void
46
     */
47 67
    private function processConfig(string $path, array $config) : void
48
    {
49 67
        foreach ($config as $key => $value) {
50 58
            $this->pathMap[$path.$key] = $value;
51
52 58
            if (is_array($value) && !empty($value)) {
53 58
                $this->processConfig($path.$key.'/', $value);
54
            }
55
        }
56 67
    }
57
58
    /**
59
     * Checks whether the key-path does exist or not.
60
     *
61
     * @param string $path
62
     * @return bool
63
     */
64 56
    public function has(string $path) : bool
65
    {
66 56
        return isset($this->pathMap[$path]);
67
    }
68
69
    /**
70
     * Retrieves configuration data for a specific key.
71
     *
72
     * @param string $path
73
     * @throws InvalidArgumentException
74
     * @return array
75
     */
76 55
    public function getData(string $path) : array
77
    {
78 55
        if (!$this->has($path)) {
79 1
            throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path), 1000);
80
        }
81
82 54
        $data = $this->pathMap[$path];
83
84 54
        return is_array($data) ? $data : [$data];
85
    }
86
87
    /**
88
     * Returns the configuration instance for a specific key. Also add the possibility to merge additional information
89
     * into it.
90
     *
91
     * @param string $path
92
     * @throws InvalidArgumentException
93
     * @return ServiceInterface
94
     */
95 36
    public function getConfig(string $path) : ServiceInterface
96
    {
97 36
        if (!$this->has($path)) {
98 1
            throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path), 1001);
99
        }
100
101 35
        return new self($this->getData($path));
102
    }
103
104
    /**
105
     * Returns the stored raw config array.
106
     *
107
     * @return array
108
     */
109 6
    public function toArray() : array
110
    {
111 6
        return $this->rawConfig;
112
    }
113
}
114