Completed
Push — master ( fa909b...9344a1 )
by Beñat
01:48
created

fromStoredEventsToDomainEvents()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 3
eloc 12
nc 3
nop 1
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();
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $storedEventsCollection (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
        $domainEvents = $this->fromStoredEventsToDomainEvents($storedEvents);
0 ignored issues
show
Unused Code introduced by
$domainEvents is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
51
        return new Stream($name, $events);
0 ignored issues
show
Bug introduced by
The variable $events does not exist. Did you mean $storedEventsCollection?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The method type() does not seem to exist on object<LIN3S\SharedKernel\Event\StoredEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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