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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace LIN3S\SharedKernel\Infrastructure\Persistence\Doctrine\ORM\Event; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityRepository; |
17
|
|
|
use LIN3S\SharedKernel\Domain\Model\DomainEventCollection; |
18
|
|
|
use LIN3S\SharedKernel\Event\EventStore; |
19
|
|
|
use LIN3S\SharedKernel\Event\StoredEvent; |
20
|
|
|
use LIN3S\SharedKernel\Event\Stream; |
21
|
|
|
use LIN3S\SharedKernel\Event\StreamName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Beñat Espiña <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class DoctrineORMEventStore extends EntityRepository implements EventStore |
27
|
|
|
{ |
28
|
|
|
public function append(Stream $stream) : void |
29
|
|
|
{ |
30
|
|
|
foreach ($stream->events() as $event) { |
31
|
|
|
$this->getEntityManager()->persist( |
32
|
|
|
StoredEvent::fromDomainEvent($event, $stream) |
|
|
|
|
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function streamOfName(StreamName $name) : Stream |
38
|
|
|
{ |
39
|
|
|
$storedEventsCollection = $this->findBy(['stream_name' => $name->name()]); |
40
|
|
|
$storedEvents = $storedEventsCollection->toArray(); |
|
|
|
|
41
|
|
|
$domainEvents = $this->fromStoredEventsToDomainEvents($storedEvents); |
|
|
|
|
42
|
|
|
|
43
|
|
|
return new Stream($name, $events); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function fromStoredEventsToDomainEvents(StoredEvent ...$storedEvents) : DomainEventCollection |
47
|
|
|
{ |
48
|
|
|
$domainEvents = new DomainEventCollection(); |
49
|
|
|
foreach ($storedEvents as $storedEvent) { |
50
|
|
|
$eventType = $storedEvent->type(); |
|
|
|
|
51
|
|
|
$payload = json_decode($storedEvent['payload'], true); |
52
|
|
|
|
53
|
|
|
$eventReflection = new \ReflectionClass($eventType); |
54
|
|
|
$domainEvent = $eventReflection->newInstanceWithoutConstructor(); |
55
|
|
|
foreach ($eventReflection->getProperties() as $property) { |
56
|
|
|
$property->setAccessible(true); |
57
|
|
|
if (isset($payload[$property->name])) { |
58
|
|
|
$property->setValue($domainEvent, $payload[$property->name]); |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
$property->setValue($domainEvent, $storedEventRow[$property]); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$domainEvents->add($domainEvent); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $domainEvents; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: