MessageProperties::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

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
nc 1
nop 2
crap 1
1
<?php
2
3
namespace TreeHouse\Queue\Message;
4
5
class MessageProperties implements \ArrayAccess
6
{
7
    const CONTENT_TYPE_BASIC = 'application/octet-stream';
8
    const CONTENT_TYPE_TEXT_PLAIN = 'text/plain';
9
10
    const DELIVERY_MODE_NON_PERSISTENT = 1;
11
    const DELIVERY_MODE_PERSISTENT = 2;
12
13
    const KEY_CONTENT_TYPE = 'content_type';
14
    const KEY_DELIVERY_MODE = 'delivery_mode';
15
    const KEY_PRIORITY = 'priority';
16
    const KEY_TIMESTAMP = 'timestamp';
17
    const KEY_HEADERS = 'headers';
18
19
    const KEY_DELAY = 'x-delay';
20
21
    /**
22
     * @var array
23
     */
24
    private $properties = [];
25
26
    /**
27
     * @param array $properties Some default properties will be set if undefined:
28
     *                          content_type = text/plain and delivery_mode = 2 (persistent)
29
     */
30 26
    public function __construct(array $properties = [])
31
    {
32 26
        foreach ($properties as $key => $value) {
33 18
            $this->set($key, $value);
34
        }
35 26
    }
36
37
    /**
38
     * @param string $key
39
     * @param mixed  $value
40
     */
41 24
    public function set($key, $value)
42
    {
43 24
        $this->properties[$key] = $value;
44 24
    }
45
46
    /**
47
     * @param string $key
48
     *
49
     * @return bool
50
     */
51 9
    public function has($key)
52
    {
53 9
        return array_key_exists($key, $this->properties);
54
    }
55
56
    /**
57
     * @param string $key
58
     *
59
     * @throws \OutOfBoundsException If the key does not exist
60
     *
61
     * @return mixed
62
     */
63 14
    public function get($key)
64
    {
65 14
        if (!array_key_exists($key, $this->properties)) {
66 1
            throw new \OutOfBoundsException(sprintf('Key "%s" does not exist', $key));
67
        }
68
69 13
        return $this->properties[$key];
70
    }
71
72
    /**
73
     * @param string $key
74
     */
75 2
    public function remove($key)
76
    {
77 2
        unset($this->properties[$key]);
78 2
    }
79
80
    /**
81
     * @return array
82
     */
83 4
    public function toArray()
84
    {
85 4
        return $this->properties;
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 5
    public function offsetExists($offset)
92
    {
93 5
        return $this->has($offset);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 5
    public function offsetGet($offset)
100
    {
101 5
        return $this->get($offset);
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 3
    public function offsetSet($offset, $value)
108
    {
109 3
        $this->set($offset, $value);
110 3
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 1
    public function offsetUnset($offset)
116
    {
117 1
        $this->remove($offset);
118 1
    }
119
}
120