ExecutionContext::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Context;
6
7
class ExecutionContext implements \ArrayAccess
8
{
9
    /**
10
     * @var array
11
     */
12
    private $data;
13
14
    /**
15
     * @param array $data The context data.
16
     */
17
    public function __construct(array $data = [])
18
    {
19
        $this->data = $data;
20
    }
21
22
    public function get(string $key, $default = null)
23
    {
24
        if (isset($this->data[$key])) {
25
            return $this->data[$key];
26
        }
27
28
        return $default;
29
    }
30
31
    /**
32
     * Get a data.
33
     *
34
     * @param string $key Key.
35
     *
36
     * @return mixed
37
     */
38
    public function offsetGet($key)
39
    {
40
        if (!array_key_exists($key, $this->data)) {
41
            throw new \RuntimeException(sprintf('Identifier "%s" does not exist in the context.', $key));
42
        }
43
44
        return $this->data[$key];
45
    }
46
47
    /**
48
     * Check if a data exists.
49
     *
50
     * @param string $key Key.
51
     *
52
     * @return bool
53
     */
54
    public function offsetExists($key)
55
    {
56
        return array_key_exists($key, $this->data);
57
    }
58
59
    /**
60
     * Set a data.
61
     *
62
     * @param string $key   Key.
63
     * @param mixed  $value Value.
64
     */
65
    public function offsetSet($key, $value)
66
    {
67
        throw new \LogicException('The execution context is read-only.');
68
    }
69
70
    /**
71
     * Unset a data.
72
     *
73
     * @param string $key Key.
74
     */
75
    public function offsetUnset($key)
76
    {
77
        throw new \LogicException('The execution context is read-only.');
78
    }
79
}
80