1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2012-2014 Janos Szurovecz |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
7
|
|
|
* the Software without restriction, including without limitation the rights to |
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
10
|
|
|
* so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
13
|
|
|
* copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
21
|
|
|
* SOFTWARE. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace predaddy\inmemory; |
25
|
|
|
|
26
|
|
|
use ArrayIterator; |
27
|
|
|
use Countable; |
28
|
|
|
use Iterator; |
29
|
|
|
use predaddy\domain\AggregateId; |
30
|
|
|
use predaddy\domain\DomainEvent; |
31
|
|
|
use predaddy\domain\eventsourcing\AbstractSnapshotEventStore; |
32
|
|
|
use predaddy\domain\eventsourcing\EventSourcedAggregateRoot; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @author Janos Szurovecz <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
final class InMemoryEventStore extends AbstractSnapshotEventStore |
38
|
|
|
{ |
39
|
|
|
private $events = []; |
40
|
|
|
private $snapshots = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param EventSourcedAggregateRoot $aggregateRoot |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function doCreateSnapshot(EventSourcedAggregateRoot $aggregateRoot) |
47
|
|
|
{ |
48
|
|
|
$this->snapshots[$this->createKey($aggregateRoot->getId())] = $aggregateRoot; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param DomainEvent $event |
53
|
|
|
* @return int version number |
54
|
|
|
*/ |
55
|
|
|
protected function doPersist(DomainEvent $event) |
56
|
|
|
{ |
57
|
|
|
self::getLogger()->debug('Persisting event into inmemory event store [{}]...', [$event]); |
58
|
|
|
$key = $this->createKey($event->aggregateId()); |
59
|
|
|
$this->events[$key][] = $event; |
60
|
|
|
return count($this->events[$key]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Must be return all events stored to aggregate identified by $aggregateId and $type. |
65
|
|
|
* Events must be ordered by theirs persistent time. |
66
|
|
|
* |
67
|
|
|
* If the $stateHash parameter is set, the result will contain only the newer DomainEvents. |
68
|
|
|
* |
69
|
|
|
* @param AggregateId $aggregateId |
70
|
|
|
* @param string $stateHash State hash |
71
|
|
|
* @return Iterator|Countable |
72
|
|
|
*/ |
73
|
|
|
public function getEventsFor(AggregateId $aggregateId, $stateHash = null) |
74
|
|
|
{ |
75
|
|
|
$result = []; |
76
|
|
|
$add = false; |
77
|
|
|
$key = $this->createKey($aggregateId); |
78
|
|
|
$events = array_key_exists($key, $this->events) ? $this->events[$key] : []; |
79
|
|
|
/* @var $event DomainEvent */ |
80
|
|
|
foreach ($events as $event) { |
81
|
|
|
if ($add || $stateHash === null) { |
82
|
|
|
$result[] = $event; |
83
|
|
|
} elseif ($event->stateHash() === $stateHash) { |
84
|
|
|
$add = true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
self::getLogger()->debug( |
88
|
|
|
'Events for aggregate [{}] with state hash [{}] has been loaded', |
89
|
|
|
[$aggregateId, $stateHash] |
90
|
|
|
); |
91
|
|
|
return new ArrayIterator($result); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param AggregateId $aggregateId |
96
|
|
|
* @return EventSourcedAggregateRoot|null |
97
|
|
|
*/ |
98
|
|
|
public function loadSnapshot(AggregateId $aggregateId) |
99
|
|
|
{ |
100
|
|
|
self::getLogger()->debug('Loading snapshot for aggregate [{}]...', [$aggregateId]); |
101
|
|
|
$key = $this->createKey($aggregateId); |
102
|
|
|
return array_key_exists($key, $this->snapshots) |
103
|
|
|
? $this->snapshots[$key] |
104
|
|
|
: null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function clean() |
108
|
|
|
{ |
109
|
|
|
$this->events = []; |
110
|
|
|
$this->snapshots = []; |
111
|
|
|
self::getLogger()->debug('Inmemory event store has been cleared'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function createKey(AggregateId $aggregateId) |
115
|
|
|
{ |
116
|
|
|
return $aggregateId->aggregateClass() . $aggregateId->value(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|