ExecutionContext   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A offsetGet() 0 8 2
A offsetExists() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
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