1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* The software is based on the Axon Framework project which is |
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
18
|
|
|
* see <http://www.axonframework.org/>. |
19
|
|
|
* |
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
21
|
|
|
* and is licensed under the MIT license. For more information, see |
22
|
|
|
* <http://www.governor-framework.org/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Governor\Framework\CommandHandling\Distributed; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
use Governor\Framework\CommandHandling\CommandMessageInterface; |
29
|
|
|
use Governor\Framework\CommandHandling\GenericCommandMessage; |
30
|
|
|
use Governor\Framework\Domain\MetaData; |
31
|
|
|
use Governor\Framework\Serializer\SerializerInterface; |
32
|
|
|
use Governor\Framework\Serializer\MessageSerializer; |
33
|
|
|
use Governor\Framework\Serializer\SimpleSerializedObject; |
34
|
|
|
use Governor\Framework\Serializer\SimpleSerializedType; |
35
|
|
|
|
36
|
|
|
class DispatchMessage |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $commandName; |
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $commandIdentifier; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
private $expectReply; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $payloadType; |
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $payloadRevision; |
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
|
64
|
|
|
private $serializedPayload; |
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $serializedMetaData; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var CommandMessageInterface |
72
|
|
|
*/ |
73
|
|
|
private $commandMessage; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param CommandMessageInterface $commandMessage |
77
|
|
|
* @param SerializerInterface $serializer |
78
|
|
|
* @param bool $expectReply |
79
|
|
|
*/ |
80
|
3 |
|
public function __construct(CommandMessageInterface $commandMessage, SerializerInterface $serializer, $expectReply) |
81
|
|
|
{ |
82
|
3 |
|
$this->commandMessage = $commandMessage; |
83
|
3 |
|
$this->commandIdentifier = $commandMessage->getIdentifier(); |
84
|
3 |
|
$this->expectReply = $expectReply; |
85
|
|
|
|
86
|
3 |
|
$messageSerializer = new MessageSerializer($serializer); |
87
|
|
|
|
88
|
3 |
|
$payload = $messageSerializer->serializePayload($commandMessage); |
89
|
3 |
|
$metaData = $messageSerializer->serializeMetaData($commandMessage); |
90
|
|
|
|
91
|
3 |
|
$this->payloadType = $payload->getType()->getName(); |
92
|
3 |
|
$this->payloadRevision = $payload->getType()->getRevision(); |
93
|
|
|
|
94
|
3 |
|
$this->serializedPayload = $payload->getData(); |
95
|
3 |
|
$this->serializedMetaData = $metaData->getData(); |
96
|
3 |
|
$this->commandName = $commandMessage->getCommandName(); |
97
|
3 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function getCommandIdentifier() |
103
|
|
|
{ |
104
|
1 |
|
return $this->commandIdentifier; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return boolean |
109
|
|
|
*/ |
110
|
1 |
|
public function isExpectReply() |
111
|
|
|
{ |
112
|
1 |
|
return $this->expectReply; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the CommandMessage wrapped in this Message. |
117
|
|
|
* |
118
|
|
|
* @return CommandMessageInterface the CommandMessage wrapped in this Message |
119
|
|
|
*/ |
120
|
1 |
|
public function getCommandMessage() |
121
|
|
|
{ |
122
|
1 |
|
return $this->commandMessage; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
3 |
|
public function toBytes() |
129
|
|
|
{ |
130
|
3 |
|
$data = pack( |
131
|
3 |
|
sprintf("Na%sa36n", strlen($this->commandName)), |
132
|
3 |
|
strlen($this->commandName), |
133
|
3 |
|
$this->commandName, |
134
|
3 |
|
$this->commandIdentifier, |
135
|
3 |
|
$this->expectReply ? 1 : 0 |
136
|
3 |
|
); |
137
|
|
|
|
138
|
|
|
// TODO payload revision |
139
|
3 |
|
$packFormat = sprintf( |
140
|
3 |
|
"Na%sNa%sNa%s", |
141
|
3 |
|
strlen($this->payloadType), |
142
|
3 |
|
strlen($this->serializedPayload), |
143
|
3 |
|
strlen($this->serializedMetaData) |
144
|
3 |
|
); |
145
|
|
|
|
146
|
3 |
|
$data .= pack( |
147
|
3 |
|
$packFormat, |
148
|
3 |
|
strlen($this->payloadType), |
149
|
3 |
|
$this->payloadType, |
150
|
3 |
|
strlen($this->serializedPayload), |
151
|
3 |
|
$this->serializedPayload, |
152
|
3 |
|
strlen($this->serializedMetaData), |
153
|
3 |
|
$this->serializedMetaData |
154
|
3 |
|
); |
155
|
|
|
|
156
|
3 |
|
return $data; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param SerializerInterface $serializer The serialize to deserialize message contents with |
161
|
|
|
* @param mixed $data |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
1 |
|
public static function fromBytes(SerializerInterface $serializer, $data) |
165
|
|
|
{ |
166
|
1 |
|
$raw = unpack("NcommandNameLength", $data); |
167
|
1 |
|
$offset = 4; |
168
|
|
|
|
169
|
1 |
|
$raw = array_merge( |
170
|
1 |
|
$raw, |
171
|
1 |
|
unpack( |
172
|
1 |
|
sprintf("a%scommandName/a36commandIdentifier/nexpectReply", $raw['commandNameLength']), |
173
|
1 |
|
substr($data, $offset) |
174
|
1 |
|
) |
175
|
1 |
|
); |
176
|
1 |
|
$offset += $raw['commandNameLength'] + 36 + 2; |
177
|
|
|
|
178
|
1 |
|
self::read($raw, $offset, $data, "payloadType"); |
179
|
1 |
|
self::read($raw, $offset, $data, "payload"); |
180
|
1 |
|
self::read($raw, $offset, $data, "meta"); |
181
|
|
|
|
182
|
1 |
|
$payload = $serializer->deserialize( |
183
|
1 |
|
new SimpleSerializedObject($raw['payload'], new SimpleSerializedType($raw['payloadType'])) |
184
|
1 |
|
); |
185
|
1 |
|
$metaData = $serializer->deserialize( |
186
|
1 |
|
new SimpleSerializedObject($raw['meta'], new SimpleSerializedType(MetaData::class)) |
187
|
1 |
|
); |
188
|
|
|
|
189
|
1 |
|
return new self( |
190
|
1 |
|
new GenericCommandMessage($payload, $metaData, $raw['commandIdentifier'], $raw['commandName']), |
191
|
1 |
|
$serializer, |
192
|
1 |
|
$raw['expectReply'] === 1 ? true : false |
193
|
1 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param mixed $raw |
198
|
|
|
* @param int $offset |
199
|
|
|
* @param mixed $data |
200
|
|
|
* @param string $name |
201
|
|
|
*/ |
202
|
1 |
View Code Duplication |
private static function read(&$raw, &$offset, $data, $name) |
|
|
|
|
203
|
|
|
{ |
204
|
1 |
|
$raw = array_merge( |
205
|
1 |
|
$raw, |
206
|
1 |
|
unpack(sprintf("N%sLength", $name), substr($data, $offset)) |
207
|
1 |
|
); |
208
|
1 |
|
$offset += 4; |
209
|
|
|
|
210
|
1 |
|
$raw = array_merge( |
211
|
1 |
|
$raw, |
212
|
1 |
|
unpack( |
213
|
1 |
|
sprintf("a%s%s", $raw[sprintf("%sLength", $name)], $name), |
214
|
1 |
|
substr($data, $offset) |
215
|
1 |
|
) |
216
|
1 |
|
); |
217
|
1 |
|
$offset += $raw[sprintf("%sLength", $name)]; |
218
|
1 |
|
} |
219
|
|
|
|
220
|
|
|
} |
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.