Completed
Push — master ( d92413...ff1eea )
by Beñat
01:42
created

fromStoredEventsToDomainEvents()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
rs 8.7972
c 1
b 0
f 0
cc 4
eloc 15
nc 4
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
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)
0 ignored issues
show
Documentation introduced by
$stream is of type object<LIN3S\SharedKernel\Event\Stream>, but the function expects a object<LIN3S\SharedKernel\Event\StreamName>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
            );
34
        }
35
    }
36
37
    public function streamOfName(StreamName $name) : Stream
38
    {
39
        $storedEventsCollection = $this->findBy(['stream_name' => $name->name()]);
40
        $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...
41
        $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...
42
43
        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...
44
    }
45
46
    private function fromStoredEventsToDomainEvents(StoredEvent ...$storedEvents) : DomainEventCollection
47
    {
48
        $domainEvents = new DomainEventCollection();
49
        foreach ($storedEvents as $storedEvent) {
50
            $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...
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]);
0 ignored issues
show
Bug introduced by
The variable $storedEventRow does not exist. Did you mean $storedEvent?

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...
62
            }
63
64
            $domainEvents->add($domainEvent);
65
        }
66
67
        return $domainEvents;
68
    }
69
}
70