|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple EventStore Manager package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace SimpleEventStoreManager\Infrastructure\Persistence; |
|
12
|
|
|
|
|
13
|
|
|
use MongoDB\Database; |
|
14
|
|
|
use MongoDB\Model\BSONDocument; |
|
15
|
|
|
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface; |
|
16
|
|
|
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface; |
|
17
|
|
|
use SimpleEventStoreManager\Domain\Model\Event; |
|
18
|
|
|
use SimpleEventStoreManager\Domain\Model\AggregateUuid; |
|
19
|
|
|
|
|
20
|
|
|
class MongoEventStoreRepository implements EventStoreRepositoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Database |
|
24
|
|
|
*/ |
|
25
|
|
|
private $mongo; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \MongoDB\Collection |
|
29
|
|
|
*/ |
|
30
|
|
|
private $events; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* MongoEventAggregateRepository constructor. |
|
34
|
|
|
* @param Database $mongo |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Database $mongo) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->mongo = $mongo; |
|
39
|
|
|
$this->events = $this->mongo->events; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param AggregateUuid $uuid |
|
44
|
|
|
* @param int $returnType |
|
45
|
|
|
* |
|
46
|
|
|
* @return array|null |
|
47
|
|
|
*/ |
|
48
|
|
|
public function byUuid(AggregateUuid $uuid, $returnType = self::RETURN_AS_ARRAY) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($document = $this->events->find(['uuid' => (string) $uuid])->toArray()) { |
|
51
|
|
|
return $this->buildEventAggregate($document, $returnType); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param $document |
|
59
|
|
|
* @param $returnType |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
private function buildEventAggregate($document, $returnType) |
|
64
|
|
|
{ |
|
65
|
|
|
if ($returnType === self::RETURN_AS_ARRAY) { |
|
66
|
|
|
return $this->buildAggregateAsArray($document); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->buildAggregateAsObject($document); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param BSONDocument $document |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
private function buildAggregateAsArray($document) |
|
78
|
|
|
{ |
|
79
|
|
|
$returnArray = []; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($document as $event) { |
|
82
|
|
|
$returnArray[] = [ |
|
83
|
|
|
'uuid' => (string) $event->uuid, |
|
84
|
|
|
'version' => $event->version, |
|
85
|
|
|
'payload' => $event->payload, |
|
86
|
|
|
'type' => $event->type, |
|
87
|
|
|
'body' => unserialize($event->body), |
|
88
|
|
|
'occurred_on' => $event->occurred_on |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $returnArray; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param BSONDocument $document |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
private function buildAggregateAsObject($document) |
|
101
|
|
|
{ |
|
102
|
|
|
$returnObject = []; |
|
103
|
|
|
|
|
104
|
|
|
foreach ($document as $event) { |
|
105
|
|
|
$returnObject[] = new Event( |
|
106
|
|
|
new AggregateUuid($event->uuid), |
|
107
|
|
|
$event->type, |
|
108
|
|
|
unserialize($event->body), |
|
109
|
|
|
$event->version, |
|
110
|
|
|
$event->occurred_on |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $returnObject; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param AggregateUuid $uuid |
|
119
|
|
|
* |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
public function count(AggregateUuid $uuid) |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->events->count(['uuid' => (string) $uuid]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param EventInterface $event |
|
129
|
|
|
*/ |
|
130
|
|
|
public function save(EventInterface $event) |
|
131
|
|
|
{ |
|
132
|
|
|
$uuid = (string) $event->uuid(); |
|
133
|
|
|
$version = ($this->count($event->uuid())) ?: 0; |
|
134
|
|
|
$payload = $event->payload(); |
|
135
|
|
|
$type = $event->type(); |
|
136
|
|
|
$body = serialize($event->body()); |
|
137
|
|
|
$occurredOn = $event->occurredOn()->format('Y-m-d H:i:s.u'); |
|
138
|
|
|
|
|
139
|
|
|
$this->events->insertOne([ |
|
140
|
|
|
'uuid' => $uuid, |
|
141
|
|
|
'version' => $version, |
|
142
|
|
|
'payload' => $payload, |
|
143
|
|
|
'type' => $type, |
|
144
|
|
|
'body' => $body, |
|
145
|
|
|
'occurred_on' => $occurredOn |
|
146
|
|
|
]); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|