Passed
Pull Request — master (#277)
by Kirill
03:11
created

InjectableConfig   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetSet() 0 4 1
A getIterator() 0 3 1
A toArray() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 7 2
A __construct() 0 3 1
A __set_state() 0 3 1
A offsetUnset() 0 4 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Core;
13
14
use Spiral\Core\Container\InjectableInterface;
15
use Spiral\Core\Exception\ConfigException;
16
17
/**
18
 * Generic implementation of array based configuration.
19
 */
20
abstract class InjectableConfig implements InjectableInterface, \IteratorAggregate, \ArrayAccess
21
{
22
    public const INJECTOR = ConfigsInterface::class;
23
24
    /**
25
     * Configuration data.
26
     *
27
     * @var array
28
     */
29
    protected $config = [];
30
31
    /**
32
     * At this moment on array based configs can be supported.
33
     *
34
     * @param array $config
35
     */
36
    public function __construct(array $config = [])
37
    {
38
        $this->config = $config;
39
    }
40
41
    /**
42
     * Restoring state.
43
     *
44
     * @param array $an_array
45
     *
46
     * @return static
47
     */
48
    public static function __set_state($an_array)
49
    {
50
        return new static($an_array['config']);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function toArray(): array
57
    {
58
        return $this->config;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function offsetExists($offset)
65
    {
66
        return array_key_exists($offset, $this->config);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function offsetGet($offset)
73
    {
74
        if (!$this->offsetExists($offset)) {
75
            throw new ConfigException("Undefined configuration key '{$offset}'");
76
        }
77
78
        return $this->config[$offset];
79
    }
80
81
    /**
82
     *{@inheritdoc}
83
     *
84
     * @throws ConfigException
85
     */
86
    public function offsetSet($offset, $value): void
87
    {
88
        throw new ConfigException(
89
            'Unable to change configuration data, configs are treated as immutable by default'
90
        );
91
    }
92
93
    /**
94
     *{@inheritdoc}
95
     *
96
     * @throws ConfigException
97
     */
98
    public function offsetUnset($offset): void
99
    {
100
        throw new ConfigException(
101
            'Unable to change configuration data, configs are treated as immutable by default'
102
        );
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getIterator()
109
    {
110
        return new \ArrayIterator($this->config);
111
    }
112
}
113