|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\EventSourcing\EventStore; |
|
13
|
|
|
|
|
14
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap; |
|
15
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayList; |
|
16
|
|
|
use Cubiche\Core\Specification\Criteria; |
|
17
|
|
|
use Cubiche\Domain\EventSourcing\DomainEventInterface; |
|
18
|
|
|
use Cubiche\Domain\EventSourcing\Versioning\Version; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* InMemoryEventStore class. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class InMemoryEventStore implements EventStoreInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ArrayHashMap |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $store; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* InMemoryEventStore constructor. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->store = new ArrayHashMap(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function persist(EventStream $eventStream) |
|
44
|
|
|
{ |
|
45
|
|
|
$version = 0; |
|
46
|
|
|
if (!$this->store->containsKey($eventStream->streamName())) { |
|
47
|
|
|
$this->store->set($eventStream->streamName(), new ArrayList()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** @var ArrayList $streamCollection */ |
|
51
|
|
|
$streamCollection = $this->store->get($eventStream->streamName()); |
|
52
|
|
|
|
|
53
|
|
|
/** @var DomainEventInterface $event */ |
|
54
|
|
|
foreach ($eventStream->events() as $event) { |
|
55
|
|
|
$streamCollection->add($event); |
|
56
|
|
|
|
|
57
|
|
|
$version = $event->version(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $version; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function remove($streamName) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->store->removeAt($streamName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function load($streamName, $version = 0) |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var ArrayList $streamCollection */ |
|
77
|
|
|
$streamCollection = $this->store->get($streamName); |
|
78
|
|
|
|
|
79
|
|
|
if ($streamCollection !== null) { |
|
80
|
|
|
$events = $streamCollection->find(Criteria::method('version')->gte($version))->toArray(); |
|
81
|
|
|
if (count($events) > 0) { |
|
82
|
|
|
/** @var DomainEventInterface $event */ |
|
83
|
|
|
$event = $events[0]; |
|
84
|
|
|
|
|
85
|
|
|
return new EventStream($streamName, $event->aggregateId(), $events); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|