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
|
|
|
|
40
|
|
View Code Duplication |
if ($type == Builder::EVENT_PROFILE_UPDATE) { |
|
|
|
|
41
|
|
|
if (!is_null($sequenceId) && !is_string($sequenceId)) { |
42
|
|
|
throw new \InvalidArgumentException('Sequence ID must be string or null'); |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
if (!is_string($sequenceId)) { |
46
|
|
|
throw new \InvalidArgumentException('Sequence ID must be string'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
foreach ($inodes as $node) { |
50
|
|
|
if (!$node instanceof IdentityNodeInterface) { |
51
|
|
|
throw new \InvalidArgumentException('Array of IdentityNode expected'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->type = $type; |
56
|
|
|
$this->sequenceId = $sequenceId; |
57
|
|
|
$this->inodes = $inodes; |
58
|
|
|
$this->data = $values; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function getIterator() |
65
|
|
|
{ |
66
|
|
|
return new \ArrayIterator($this->data); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function getType() |
73
|
|
|
{ |
74
|
|
|
return $this->type; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
*/ |
80
|
|
|
public function getSequenceId() |
81
|
|
|
{ |
82
|
|
|
return $this->sequenceId; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function getIdentities() |
89
|
|
|
{ |
90
|
|
|
return $this->inodes; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function offsetExists($offset) |
97
|
|
|
{ |
98
|
|
|
return array_key_exists($offset, $this->data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritDoc |
103
|
|
|
*/ |
104
|
|
|
public function offsetGet($offset) |
105
|
|
|
{ |
106
|
|
|
if (!$this->offsetExists($offset)) { |
107
|
|
|
throw new \OutOfBoundsException("No offset {$offset}"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->data[$offset]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function offsetSet($offset, $value) |
117
|
|
|
{ |
118
|
|
|
$this->data[$offset] = $value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
124
|
|
|
public function offsetUnset($offset) |
125
|
|
|
{ |
126
|
|
|
if ($this->offsetExists($offset)) { |
127
|
|
|
unset($this->data[$offset]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
|
|
public function count() |
135
|
|
|
{ |
136
|
|
|
return count($this->data); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritDoc |
141
|
|
|
*/ |
142
|
|
|
public function toArray() |
143
|
|
|
{ |
144
|
|
|
return $this->data; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.