Completed
Branch master (a35590)
by Anton
04:28 queued 01:35
created

Envelope::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 12
nc 5
nop 4
1
<?php
2
3
namespace Covery\Client\Envelopes;
4
5
use Covery\Client\EnvelopeInterface;
6
use Covery\Client\IdentityNodeInterface;
7
8
class Envelope implements EnvelopeInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $sequenceId;
14
    /**
15
     * @var string
16
     */
17
    private $type;
18
    /**
19
     * @var array
20
     */
21
    private $data;
22
    /**
23
     * @var IdentityNodeInterface[]
24
     */
25
    private $inodes;
26
27
    /**
28
     * Envelope constructor.
29
     * @param string $type
30
     * @param string $sequenceId
31
     * @param IdentityNodeInterface[] $inodes
32
     * @param array $values
33
     */
34
    public function __construct($type, $sequenceId, array $inodes, array $values)
35
    {
36
        if (!is_string($type)) {
37
            throw new \InvalidArgumentException('Envelope type must be string');
38
        }
39
        if (!is_string($sequenceId)) {
40
            throw new \InvalidArgumentException('Sequence ID must be string');
41
        }
42
        foreach ($inodes as $node) {
43
            if (!$node instanceof IdentityNodeInterface) {
44
                throw new \InvalidArgumentException('Array of IdentityNode expected');
45
            }
46
        }
47
48
        $this->type = $type;
49
        $this->sequenceId = $sequenceId;
50
        $this->inodes = $inodes;
51
        $this->data = $values;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function getIterator()
58
    {
59
        return new \ArrayIterator($this->data);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function getType()
66
    {
67
        return $this->type;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function getSequenceId()
74
    {
75
        return $this->sequenceId;
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function getIdentities()
82
    {
83
        return $this->inodes;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function offsetExists($offset)
90
    {
91
        return array_key_exists($offset, $this->data);
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function offsetGet($offset)
98
    {
99
        if (!$this->offsetExists($offset)) {
100
            throw new \OutOfBoundsException("No offset {$offset}");
101
        }
102
103
        return $this->data[$offset];
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function offsetSet($offset, $value)
110
    {
111
        $this->data[$offset] = $value;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function offsetUnset($offset)
118
    {
119
        if ($this->offsetExists($offset)) {
120
            unset($this->data[$offset]);
121
        }
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127
    public function toArray()
128
    {
129
        return $this->data;
130
    }
131
}
132