forked from broadway/broadway
/
EventSourcingRepository.php
104 lines (87 loc) · 3.18 KB
/
EventSourcingRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* This file is part of the broadway/broadway package.
*
* (c) Qandidate.com <opensource@qandidate.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Broadway\EventSourcing;
use Assert\Assertion as Assert;
use Assert\InvalidArgumentException;
use Broadway\Domain\AggregateRoot;
use Broadway\Domain\DomainEventStream;
use Broadway\EventHandling\EventBusInterface;
use Broadway\EventStore\EventStoreInterface;
use Broadway\EventStore\EventStreamNotFoundException;
use Broadway\Repository\AggregateNotFoundException;
use Broadway\Repository\RepositoryInterface;
/**
* Naive initial implementation of an event sourced aggregate repository.
*/
class EventSourcingRepository implements RepositoryInterface
{
private $eventStore;
private $eventBus;
private $aggregateClass;
/**
* @param string $aggregateClass
*/
public function __construct(
EventStoreInterface $eventStore,
EventBusInterface $eventBus,
$aggregateClass,
array $eventStreamDecorators = array()
) {
$this->assertExtendsEventSourcedAggregateRoot($aggregateClass);
$this->eventStore = $eventStore;
$this->eventBus = $eventBus;
$this->aggregateClass = $aggregateClass; // todo: aggregate factory
$this->eventStreamDecorators = $eventStreamDecorators;
}
/**
* {@inheritDoc}
*/
public function load($id)
{
try {
$domainEventStream = $this->eventStore->load($id);
$aggregate = new $this->aggregateClass();
$aggregate->initializeState($domainEventStream);
return $aggregate;
} catch (EventStreamNotFoundException $e) {
throw AggregateNotFoundException::create($id, $e);
}
}
public function add(AggregateRoot $aggregate)
{
// maybe we can get generics one day.... ;)
Assert::isInstanceOf($aggregate, $this->aggregateClass);
$domainEventStream = $aggregate->getUncommittedEvents();
$eventStream = $this->decorateForWrite($aggregate, $domainEventStream);
$this->eventStore->append($aggregate->getId(), $eventStream);
$this->eventBus->publish($domainEventStream);
}
private function decorateForWrite(AggregateRoot $aggregate, DomainEventStream $eventStream)
{
$aggregateType = $this->getType();
$aggregateIdentifier = $aggregate->getId();
foreach ($this->eventStreamDecorators as $eventStreamDecorator) {
$eventStream = $eventStreamDecorator->decorateForWrite($aggregateType, $aggregateIdentifier, $eventStream);
}
return $eventStream;
}
// todo: move to assert lib?
private function assertExtendsEventSourcedAggregateRoot($class)
{
$parents = class_parents($class);
if (! in_array('Broadway\EventSourcing\EventSourcedAggregateRoot', $parents)) {
throw new InvalidArgumentException(sprintf("Class '%s' is not an EventSourcedAggregateRoot.", $class), -1);
}
}
private function getType()
{
return $this->aggregateClass;
}
}