Passed
Push — master ( 07de51...b0405c )
by Zlatin
02:16
created

Config   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 82.5%

Importance

Changes 0
Metric Value
wmc 15
eloc 30
dl 0
loc 122
ccs 33
cts 40
cp 0.825
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 7 2
A init() 0 3 1
A load() 0 12 3
A has() 0 3 1
A __construct() 0 3 1
A all() 0 3 1
A set() 0 5 1
A loadFromArray() 0 25 5
1
<?php
2
namespace DevOp\Core;
3
4
class Config
5
{
6
7
    /**
8
     * @var mixed
9
     */
10
    private $container = [];
11
12
    /**
13
     * @param mixed $resources
14
     * @param string|null $environment
15
     * @param array|null $params
16
     */
17 10
    public function __construct($resources, $environment = null, array $params = [])
18
    {
19 10
        $this->load($resources, $environment, $params);
20 10
    }
21
22
    /**
23
     * @param string $name
24
     * @return boolean
25
     */
26 6
    public function has($name)
27
    {
28 6
        return isset($this->container[$name]);
29
    }
30
31
    /**
32
     * @param string $name
33
     * @param mixed $default
34
     * @return mixed
35
     */
36 6
    public function get($name, $default = null)
37
    {
38 6
        if ($this->has($name)) {
39 6
            return $this->container[$name];
40
        }
41
42
        return $default;
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param mixed $args
48
     */
49 2
    public function set($name, $args)
50
    {
51 2
        $this->container[$name] = $args;
52
53 2
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59 2
    public function all()
60
    {
61 2
        return $this->container;
62
    }
63
64
    /**
65
     * @param mixed $resources
66
     * @param string|null $environment
67
     * @param array $params
68
     * @return \static
69
     */
70 10
    public static function init($resources, $environment = null, array $params = [])
71
    {
72 10
        return new static($resources, $environment, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static($resou... $environment, $params) returns the type DevOp\Core\Config which is incompatible with the documented return type static.
Loading history...
73
    }
74
    
75
    /**
76
     * @param mixed $resources
77
     * @param string|null $environment
78
     * @param array $params
79
     * @throws \RuntimeException
80
     */
81 10
    public function load($resources, $environment = null, array $params = [])
82
    {
83
84 10
        if (is_array($resources)) {
85 10
            $data = $this->loadFromArray($resources, $environment);
86 10
        } else if (file_exists($resources)) {
87
            $data = $this->loadFromFile([$resources], $environment);
0 ignored issues
show
Bug introduced by
The method loadFromFile() does not exist on DevOp\Core\Config. Did you maybe mean loadFromArray()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
            /** @scrutinizer ignore-call */ 
88
            $data = $this->loadFromFile([$resources], $environment);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
        } else {
89 2
            throw new \RuntimeException('Invalid configuration source defined.');
90
        }
91
92 10
        $this->container = array_replace_recursive($data, $params);
93 10
    }
94
95
    /**
96
     * @param array $resources
97
     * @param string|null $environment
98
     * @return mixed
99
     * @throws \InvalidArgumentException
100
     */
101 10
    private function loadFromArray(array $resources = [], $environment = null)
102
    {
103 10
        $data = [];
104
105 10
        foreach ($resources AS $resource) {
106
            
107 10
            if (!file_exists($resource)) {
108
                throw new \InvalidArgumentException("Invalid resource {$resource}");
109
            }
110
111 10
            $data = array_replace_recursive($data, include $resource);
112
113 10
            if (null === $environment) {
114
                continue;
115
            }
116
117 10
            $pathinfo = pathinfo($resource);
118 10
            $filename = "{$pathinfo['filename']}_{$environment}.{$pathinfo['extension']}";
119
120 10
            if (file_exists($filename)) {
121
                $data = array_replace_recursive($data, include $filename);
122
            }
123 10
        }
124
125 10
        return $data;
126
    }
127
}
128