Completed
Branch 0.4-dev (d27253)
by Evgenij
03:26
created

ExecutionContext::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace AsyncSockets\RequestExecutor;
12
13
/**
14
 * Class ExecutionContext
15
 */
16
class ExecutionContext implements \ArrayAccess
17
{
18
    /**
19
     * Data items
20
     *
21
     * @var array
22
     */
23
    private $items;
24
25
    /**
26
     * Nested contexts
27
     *
28
     * @var ExecutionContext[]
29
     */
30
    private $namespaces;
31
32
    /**
33
     * ExecutionContext constructor.
34
     *
35
     * @param array $items Initial data for context
36
     */
37 269
    public function __construct(array $items = [])
38
    {
39 269
        $this->items      = $items;
40 269
        $this->namespaces = [];
41 269
    }
42
43
    /**
44
     * Return nested isolated execution context
45
     *
46
     * @param string $namespace Namespace name
47
     *
48
     * @return ExecutionContext
49
     */
50 8
    public function inNamespace($namespace)
51
    {
52 8
        if (!isset($this->namespaces[$namespace])) {
53 8
            $this->namespaces[$namespace] = new static([]);
54 8
        }
55
56 8
        return $this->namespaces[$namespace];
57
    }
58
59
    /**
60
     * Clears data inside the context
61
     *
62
     * @return void
63
     */
64 1
    public function clear()
65
    {
66 1
        $this->items = [];
67 1
    }
68
69
    /**
70
     * Set a value
71
     *
72
     * @param string|int $key   Key
73
     * @param mixed      $value A value
74
     *
75
     * @return void
76
     */
77 9
    public function set($key, $value)
78
    {
79 9
        $this->offsetSet($key, $value);
80 9
    }
81
82
    /**
83
     * Return a value stored under the key
84
     *
85
     * @param string|int $key A key
86
     * @param mixed      $default Default value to return if key does not exist
87
     *
88
     * @return mixed
89
     */
90 4
    public function get($key, $default = null)
91
    {
92 4
        return $this->has($key) ? $this->items[$key] : $default;
93
    }
94
95
    /**
96
     * Return true if context has value for a given key
97
     *
98
     * @param string|int $key A key
99
     *
100
     * @return bool
101
     */
102 5
    public function has($key)
103
    {
104 5
        return $this->offsetExists($key);
105
    }
106
107
    /**
108
     * Removes a value under given key
109
     *
110
     * @param string|int $key A key
111
     *
112
     * @return void
113
     */
114 1
    public function remove($key)
115
    {
116 1
        $this->offsetUnset($key);
117 1
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 5
    public function offsetExists($offset)
123
    {
124 5
        return isset($this->items[$offset]) || array_key_exists($offset, $this->items);
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 1
    public function offsetGet($offset)
131
    {
132 1
        return $this->offsetExists($offset) ? $this->items[$offset] : null;
133
    }
134
135
    /**
136
     * @inheritDoc
137
     */
138 9
    public function offsetSet($offset, $value)
139
    {
140 9
        $this->items[$offset] = $value;
141 9
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146 1
    public function offsetUnset($offset)
147
    {
148 1
        unset($this->items[$offset]);
149 1
    }
150
}
151