|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Shared Kernel library. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
|
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 LIN3S\SharedKernel\Infrastructure\Persistence\Doctrine\ORM\Event; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\ORM\EntityRepository; |
|
16
|
|
|
use LIN3S\SharedKernel\Domain\Model\DomainEvent; |
|
17
|
|
|
use LIN3S\SharedKernel\Event\StoredEvent; |
|
18
|
|
|
use LIN3S\SharedKernel\Event\Stream; |
|
19
|
|
|
use LIN3S\SharedKernel\Event\StreamName; |
|
20
|
|
|
use LIN3S\SharedKernel\Infrastructure\Persistence\Sql\Pdo; |
|
21
|
|
|
use LIN3S\SharedKernel\Domain\Model\AggregateRootDoesNotExistException; |
|
22
|
|
|
use LIN3S\SharedKernel\Domain\Model\DomainEventCollection; |
|
23
|
|
|
use LIN3S\SharedKernel\Domain\Model\Identity\Id; |
|
24
|
|
|
use LIN3S\SharedKernel\Event\EventStore; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Beñat Espiña <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class DoctrineORMEventStore extends EntityRepository implements EventStore |
|
30
|
|
|
{ |
|
31
|
|
|
public function append(Stream $stream) |
|
32
|
|
|
{ |
|
33
|
|
|
foreach ($stream->events() as $event) { |
|
34
|
|
|
$this->getEntityManager()->persist( |
|
35
|
|
|
new StoredEvent( |
|
36
|
|
|
get_class($event), |
|
37
|
|
|
$this->encodePayload($event), |
|
38
|
|
|
$event->occurredOn(), |
|
39
|
|
|
$stream->name() |
|
40
|
|
|
) |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function streamOfName(StreamName $name) |
|
46
|
|
|
{ |
|
47
|
|
|
$storedEventsCollection = $this->findBy(['stream_name' => $name->name()]); |
|
48
|
|
|
$storedEvents = $storedEventsCollection->toArray(); |
|
|
|
|
|
|
49
|
|
|
$domainEvents = $this->fromStoredEventsToDomainEvents($storedEvents); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
return new Stream($name, $events); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function encodePayload(DomainEvent $event) |
|
55
|
|
|
{ |
|
56
|
|
|
$payload = []; |
|
57
|
|
|
$eventReflection = new \ReflectionClass($event); |
|
58
|
|
|
foreach ($eventReflection->getProperties() as $property) { |
|
59
|
|
|
if ($property->name === 'occurredOn') { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$property->setAccessible(true); |
|
64
|
|
|
$payload[$property->getName()] = $property->getValue($event); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return json_encode($payload); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function fromStoredEventsToDomainEvents(StoredEvent ...$storedEvents) |
|
71
|
|
|
{ |
|
72
|
|
|
$domainEvents = new DomainEventCollection(); |
|
73
|
|
|
foreach ($storedEvents as $storedEvent) { |
|
74
|
|
|
$eventType = $storedEvent->type(); |
|
|
|
|
|
|
75
|
|
|
$payload = json_decode($storedEvent['payload'], true); |
|
76
|
|
|
|
|
77
|
|
|
$eventReflection = new \ReflectionClass($eventType); |
|
78
|
|
|
$domainEvent = $eventReflection->newInstanceWithoutConstructor(); |
|
79
|
|
|
foreach ($eventReflection->getProperties() as $property) { |
|
80
|
|
|
$property->setAccessible(true); |
|
81
|
|
|
$property->setValue($domainEvent, $payload[$property->name]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$domainEvents->add($domainEvent); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $domainEvents; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.