|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rawkode\Eidetic\EventSourcing; |
|
4
|
|
|
|
|
5
|
|
|
use Rawkode\Eidetic\CQRS\WriteModelRepository; |
|
6
|
|
|
use Rawkode\Eidetic\EventStore\EventStore; |
|
7
|
|
|
use Rawkode\Eidetic\EventStore\VersionMismatchException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Repository. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Repository implements WriteModelRepository |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var $entityClass */ |
|
15
|
|
|
private $entityClass; |
|
16
|
|
|
|
|
17
|
|
|
/** @var EventStore $eventStore */ |
|
18
|
|
|
private $eventStore; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $class |
|
22
|
|
|
* @param EventStore $eventStore |
|
23
|
|
|
*/ |
|
24
|
|
|
private function __construct($class, EventStore $eventStore) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->entityClass = $class; |
|
27
|
|
|
$this->eventStore = $eventStore; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param $class |
|
32
|
|
|
* @param EventStore $eventStore |
|
33
|
|
|
* |
|
34
|
|
|
* @return Repository |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function createForWrites($class, EventStore $eventStore) |
|
37
|
|
|
{ |
|
38
|
|
|
return new self($class, $eventStore); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $entityIdentifier |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function load($entityIdentifier) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->enforceTypeConstraint($this->eventStore->entityClass($entityIdentifier)); |
|
49
|
|
|
|
|
50
|
|
|
$events = $this->eventStore->retrieve($entityIdentifier); |
|
51
|
|
|
|
|
52
|
|
|
return call_user_func(array($this->entityClass, 'initialise'), $events); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \Exception |
|
59
|
|
|
*/ |
|
60
|
|
|
public function save(EventSourcedEntity $eventSourcedEntity) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->enforceTypeConstraint(get_class($eventSourcedEntity)); |
|
63
|
|
|
|
|
64
|
|
|
$this->enforceVersionMismatchConstraint($eventSourcedEntity); |
|
65
|
|
|
|
|
66
|
|
|
$this->eventStore->store($eventSourcedEntity); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $class |
|
71
|
|
|
* |
|
72
|
|
|
* @throws IncorrectEntityClassException |
|
73
|
|
|
*/ |
|
74
|
|
|
private function enforceTypeConstraint($class) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->entityClass !== $class) { |
|
77
|
|
|
throw new IncorrectEntityClassException(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param EventSourcedEntity $eventSourcedEntity |
|
83
|
|
|
* |
|
84
|
|
|
* @throws VersionMismatchException |
|
85
|
|
|
*/ |
|
86
|
|
|
private function enforceVersionMismatchConstraint(EventSourcedEntity $eventSourcedEntity) |
|
87
|
|
|
{ |
|
88
|
|
|
/** @var EventSourcedEntity $databaseVersion */ |
|
89
|
|
|
$databaseVersion = $this->load($eventSourcedEntity->identifier()); |
|
90
|
|
|
|
|
91
|
|
|
if ($databaseVersion->version() !== $eventSourcedEntity->version()) { |
|
92
|
|
|
throw new VersionMismatchException('Local entity is at version ' |
|
93
|
|
|
.$eventSourcedEntity->version().' and database is at '.$databaseVersion->version()); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|