Completed
Push — master ( c4ada7...915e39 )
by Matthew
01:55
created

Config::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PublishingKit\Config;
6
7
use Countable;
8
use ArrayAccess;
9
use IteratorAggregate;
10
use PublishingKit\Config\Exceptions\ConfigDoesNotExist;
11
use PublishingKit\Config\Exceptions\UnsupportedConfigFileType;
12
use PublishingKit\Config\Contracts\ConfigContainer;
13
use Symfony\Component\Yaml\Yaml;
14
use Symfony\Component\Yaml\Exception\ParseException;
15
16
/**
17
 * @psalm-immutable
18
 */
19
class Config implements ArrayAccess, Countable, IteratorAggregate, ConfigContainer
20
{
21
    /**
22
     * @var array
23
     */
24
    private $config;
25
26 60
    public function __construct(array $config)
27
    {
28 60
        $this->config = $config;
29 60
    }
30
31 15
    public static function fromFile(string $path): ConfigContainer
32
    {
33 15
        return new static(self::getFile($path));
34
    }
35
36 3
    public static function fromFiles(array $files): ConfigContainer
37
    {
38 3
        $configs = [];
39 3
        foreach ($files as $file) {
40 3
            $configs = array_merge($configs, self::getFile($file));
41
        }
42 3
        return new static($configs);
43
    }
44
45 3
    public static function __set_state(array $config): ConfigContainer
46
    {
47 3
        return new static($config);
48
    }
49
50 18
    private static function getFile(string $path): array
51
    {
52 18
        if (!file_exists($path)) {
53 3
            throw new ConfigDoesNotExist();
54
        }
55
        try {
56 15
            switch (pathinfo($path)['extension']) {
57 15
                case 'php':
58 6
                    $config = self::parseArrayFile($path);
59 6
                    break;
60 12
                case 'ini':
61 6
                    $config = self::parseIniFile($path);
62 6
                    break;
63 9
                case 'yml':
64 3
                case 'yaml':
65 6
                    $config = self::parseYamlFile($path);
66 6
                    break;
67
                default:
68 15
                    throw new UnsupportedConfigFileType(pathinfo($path)['extension']);
69
            }
70 3
        } catch (UnsupportedConfigFileType $e) {
71 3
            throw $e;
72
        }
73 12
        return $config;
74
    }
75
76 6
    private static function parseArrayFile(string $path): array
77
    {
78 6
        return include $path;
79
    }
80
81 6
    private static function parseIniFile(string $path): array
82
    {
83 6
        return parse_ini_file($path, true);
84
    }
85
86 6
    private static function parseYamlFile(string $path): array
87
    {
88 6
        return Yaml::parseFile($path);
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return string|null|Config
94
     */
95 30
    public function get(string $name)
96
    {
97 30
        if (!isset($this->config[$name])) {
98 3
            return null;
99
        }
100 27
        if (is_array($this->config[$name])) {
101 18
            return new static($this->config[$name]);
102
        }
103 24
        return $this->config[$name];
104
    }
105
106 24
    public function __get(string $name)
107
    {
108 24
        return $this->get($name);
109
    }
110
111 3
    public function has(string $name): bool
112
    {
113 3
        return isset($this->config[$name]);
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119 3
    public function count()
120
    {
121 3
        return count($this->config);
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 3
    public function offsetExists($offset)
128
    {
129 3
        return isset($this->config[$offset]);
130
    }
131
132
    /**
133
     * {@inheritDoc}
134
     */
135 6
    public function offsetGet($offset)
136
    {
137 6
        if (!isset($this->config[$offset])) {
138 3
            return null;
139
        }
140 6
        if (is_array($this->config[$offset])) {
141 3
            return new static($this->config[$offset]);
142
        }
143 6
        return $this->config[$offset];
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 3
    public function offsetSet($offset, $value)
150
    {
151 3
    }
152
153
    /**
154
     * {@inheritDoc}
155
     */
156 3
    public function offsetUnset($offset)
157
    {
158 3
    }
159
160 3
    public function toArray(): array
161
    {
162 3
        return $this->config;
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168 6
    public function getIterator()
169
    {
170 6
        return new ConfigIterator($this->config);
171
    }
172
}
173