InjectableConfig   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 96
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 8 2
A offsetSet() 0 6 1
A offsetUnset() 0 6 1
A getIterator() 0 4 1
A __set_state() 0 4 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Core;
10
11
use Spiral\Core\Exceptions\ConfigException;
12
13
/**
14
 * Generic implementation of array based configuration.
15
 *
16
 * Attention! Config has to be serialiable and be depended ONLY on environment or runtime
17
 * modifications/requests. No custom logic is allowed to initiate config, in other case config cache
18
 * will be invalid (pss... there is no config cache anymore).
19
 */
20
class InjectableConfig extends Component implements ConfigInterface, \IteratorAggregate
0 ignored issues
show
Deprecated Code introduced by
The interface Spiral\Core\ConfigInterface has been deprecated.

This class, trait or interface has been deprecated.

Loading history...
21
{
22
    /**
23
     * Spiral provides ability to automatically inject configs using configurator.
24
     */
25
    const INJECTOR = ConfiguratorInterface::class;
26
27
    /**
28
     * Configuration data.
29
     *
30
     * @var array
31
     */
32
    protected $config = [];
33
34
    /**
35
     * At this moment on array based configs can be supported.
36
     *
37
     * @param array $config
38
     */
39 25
    public function __construct(array $config = [])
40
    {
41 25
        $this->config = $config;
42 25
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function toArray(): array
48
    {
49 2
        return $this->config;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    public function offsetExists($offset)
56
    {
57 2
        return array_key_exists($offset, $this->config);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function offsetGet($offset)
64
    {
65 2
        if (!$this->offsetExists($offset)) {
66 1
            throw new ConfigException("Undefined configuration key '{$offset}'");
67
        }
68
69 1
        return $this->config[$offset];
70
    }
71
72
    /**
73
     *{@inheritdoc}
74
     *
75
     * @throws ConfigException
76
     */
77 1
    public function offsetSet($offset, $value)
78
    {
79 1
        throw new ConfigException(
80 1
            'Unable to change configuration data, configs are treated as immutable by default'
81
        );
82
    }
83
84
    /**
85
     *{@inheritdoc}
86
     *
87
     * @throws ConfigException
88
     */
89 1
    public function offsetUnset($offset)
90
    {
91 1
        throw new ConfigException(
92 1
            'Unable to change configuration data, configs are treated as immutable by default'
93
        );
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function getIterator()
100
    {
101 1
        return new \ArrayIterator($this->config);
102
    }
103
104
    /**
105
     * Restoring state.
106
     *
107
     * @param array $an_array
108
     *
109
     * @return static
110
     */
111 1
    public static function __set_state($an_array)
112
    {
113 1
        return new static($an_array['config']);
114
    }
115
}
116