-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventSourcingRepository.php
111 lines (96 loc) · 3.52 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
105
106
107
108
109
110
111
<?php
/*
* This file is part of the broadway/broadway package.
*
* (c) Qandidate.com <[email protected]>
*
* 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 Broadway\Domain\AggregateRoot;
use Broadway\Domain\DomainEventStream;
use Broadway\EventHandling\EventBusInterface;
use Broadway\EventSourcing\AggregateFactory\AggregateFactoryInterface;
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;
private $eventStreamDecorators = [];
private $aggregateFactory;
/**
* @param EventStoreInterface $eventStore
* @param EventBusInterface $eventBus
* @param string $aggregateClass
* @param AggregateFactoryInterface $aggregateFactory
* @param EventStreamDecoratorInterface[] $eventStreamDecorators
*/
public function __construct(
EventStoreInterface $eventStore,
EventBusInterface $eventBus,
$aggregateClass,
AggregateFactoryInterface $aggregateFactory,
array $eventStreamDecorators = []
) {
$this->assertExtendsEventSourcedAggregateRoot($aggregateClass);
$this->eventStore = $eventStore;
$this->eventBus = $eventBus;
$this->aggregateClass = $aggregateClass;
$this->aggregateFactory = $aggregateFactory;
$this->eventStreamDecorators = $eventStreamDecorators;
}
/**
* {@inheritDoc}
*/
public function load($id)
{
try {
$domainEventStream = $this->eventStore->load($id);
return $this->aggregateFactory->create($this->aggregateClass, $domainEventStream);
} catch (EventStreamNotFoundException $e) {
throw AggregateNotFoundException::create($id, $e);
}
}
/**
* {@inheritDoc}
*/
public function save(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->getAggregateRootId(), $eventStream);
$this->eventBus->publish($eventStream);
}
private function decorateForWrite(AggregateRoot $aggregate, DomainEventStream $eventStream)
{
$aggregateType = $this->getType();
$aggregateIdentifier = $aggregate->getAggregateRootId();
foreach ($this->eventStreamDecorators as $eventStreamDecorator) {
$eventStream = $eventStreamDecorator->decorateForWrite($aggregateType, $aggregateIdentifier, $eventStream);
}
return $eventStream;
}
private function assertExtendsEventSourcedAggregateRoot($class)
{
Assert::subclassOf(
$class,
EventSourcedAggregateRoot::class,
sprintf("Class '%s' is not an EventSourcedAggregateRoot.", $class)
);
}
private function getType()
{
return $this->aggregateClass;
}
}