ParameterBag::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace JobQueue\Domain\Task;
4
5
use JobQueue\Domain\Utils\Bag;
6
7
final class ParameterBag extends Bag
8
{
9
    /**
10
     *
11
     * @param array $data
12
     */
13 30
    public function __construct(array $data = [])
14
    {
15 30
        foreach ($data as $key => $value) {
16 21
            $this->add($key, $value);
17
        }
18 28
    }
19
20
    /**
21
     *
22
     * @param mixed $key
23
     * @return bool
24
     */
25 2
    public function has($key): bool
26
    {
27 2
        return isset($this->data[$key]);
28
    }
29
30
    /**
31
     *
32
     * @param mixed $key
33
     * @return mixed
34
     */
35 1
    public function get($key)
36
    {
37 1
        if (!$this->has($key)) {
38 1
            throw new \RuntimeException(sprintf('There is no value for "%s" key', $key));
39
        }
40
41
        return $this->data[$key];
42
    }
43
44
    /**
45
     *
46
     * @param mixed $key
47
     * @param mixed $value
48
     */
49 21
    public function add($key, $value)
50
    {
51 21
        if (!is_string($key)) {
52 1
            throw new \RuntimeException('The key must be a string');
53
        }
54
55 21
        if (!is_scalar($value) and !is_null($value)) {
56 1
            throw new \RuntimeException(sprintf('The "%s" value must be a scalar or null', $key));
57
        }
58
59 21
        $this->data[$key] = $value;
60 21
    }
61
62
    /**
63
     *
64
     * @param mixed $key
65
     */
66
    public function remove($key)
67
    {
68
        unset($this->data[$key]);
69
    }
70
}
71