|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
|
5
|
|
|
* To change this template file, choose Tools | Templates |
|
6
|
|
|
* and open the template in the editor. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Governor\Framework\EventStore\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
use Governor\Framework\Serializer\SimpleSerializedDomainEventData; |
|
12
|
|
|
use Governor\Framework\Serializer\SerializerInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Description of FilesystemEventMessageReader |
|
16
|
|
|
* |
|
17
|
|
|
* @author "David Kalosi" <[email protected]> |
|
18
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> * |
|
19
|
|
|
*/ |
|
20
|
|
|
class FilesystemEventMessageReader |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \SplFileObject |
|
25
|
|
|
*/ |
|
26
|
|
|
private $file; |
|
27
|
|
|
|
|
28
|
3 |
|
function __construct(\SplFileObject $file) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->file = $file; |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Governor\Framework\Serializer\SerializedDomainEventDataInterface|null |
|
36
|
|
|
* @throws \RuntimeException |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function readEventMessage() |
|
39
|
|
|
{ |
|
40
|
3 |
|
if (null === $len = $this->readBytes(2)) { |
|
41
|
2 |
|
return null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
$eventLength = unpack('nlength', $len); |
|
45
|
|
|
|
|
46
|
2 |
|
if (!is_integer($eventLength['length'])) { |
|
47
|
|
|
throw new \RuntimeException("Could not determine the length of the stored event"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
$message = $this->readBytes($eventLength['length']); |
|
51
|
2 |
|
$array = unpack( |
|
52
|
2 |
|
'nmagic/a36eventIdentifier/Ntimestamp/a36aggregateIdentifier/Nscn/NpayloadTypeLength', |
|
53
|
|
|
$message |
|
54
|
2 |
|
); |
|
55
|
|
|
|
|
56
|
2 |
|
$offset = 86; |
|
57
|
2 |
|
$array = array_merge( |
|
58
|
2 |
|
$array, |
|
59
|
2 |
|
unpack( |
|
60
|
2 |
|
sprintf( |
|
61
|
2 |
|
"a%spayloadType/NpayloadLength", |
|
62
|
2 |
|
$array['payloadTypeLength'] |
|
63
|
2 |
|
), |
|
64
|
2 |
|
substr($message, $offset) |
|
65
|
2 |
|
) |
|
66
|
2 |
|
); |
|
67
|
|
|
|
|
68
|
2 |
|
$offset += strlen($array['payloadType']) + 4; |
|
69
|
2 |
|
$array = array_merge( |
|
70
|
2 |
|
$array, |
|
71
|
2 |
|
unpack( |
|
72
|
2 |
|
sprintf("a%spayload/NmetaDataLength", $array['payloadLength']), |
|
73
|
2 |
|
substr($message, $offset) |
|
74
|
2 |
|
) |
|
75
|
2 |
|
); |
|
76
|
|
|
|
|
77
|
2 |
|
$offset += strlen($array['payload']) + 4; |
|
78
|
2 |
|
$array = array_merge( |
|
79
|
2 |
|
$array, |
|
80
|
2 |
|
unpack( |
|
81
|
2 |
|
sprintf("a%smetaData", $array['metaDataLength']), |
|
82
|
2 |
|
substr($message, $offset) |
|
83
|
2 |
|
) |
|
84
|
2 |
|
); |
|
85
|
|
|
|
|
86
|
|
|
// !!! TODO support for payload revision |
|
87
|
2 |
|
return new SimpleSerializedDomainEventData( |
|
88
|
2 |
|
$array['eventIdentifier'], |
|
89
|
2 |
|
$array['aggregateIdentifier'], $array['scn'], |
|
90
|
2 |
|
\DateTime::createFromFormat('U', $array['timestamp']), |
|
|
|
|
|
|
91
|
2 |
|
$array['payloadType'], null, $array['payload'], $array['metaData'] |
|
92
|
2 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
private function readBytes($length) |
|
96
|
|
|
{ |
|
97
|
3 |
|
$stream = null; |
|
98
|
3 |
|
for ($cc = 0; $cc < $length; $cc++) { |
|
99
|
3 |
|
if ($this->file->eof()) { |
|
100
|
2 |
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
3 |
|
$stream .= $this->file->fgetc(); |
|
104
|
2 |
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return $stream; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.