Completed
Pull Request — master (#279)
by Luc
13:49 queued 06:54
created

limitEventStreamToPlayhead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\EventSourcing;
4
5
use Broadway\Domain\DomainEventStream;
6
use Broadway\Domain\DomainEventStreamInterface;
7
use Broadway\Domain\DomainMessage;
8
9
class CopyAwareEventStoreDecorator extends AbstractEventStoreDecorator
10
{
11
    /**
12
     * @inheritdoc
13
     */
14
    public function load($id)
15
    {
16
        return $this->loadCompleteStream(parent::load($id));
17
    }
18
19
    /**
20
     * @param DomainEventStreamInterface $eventStream
21
     * @return DomainEventStreamInterface
22
     */
23
    private function loadCompleteStream(DomainEventStreamInterface $eventStream)
24
    {
25
        $events = iterator_to_array($eventStream);
26
        /** @var DomainMessage $oldestMessage */
27
        $oldestMessage = current($events);
28
        if (intval($oldestMessage->getPlayhead()) === 0) {
29
            return $eventStream;
30
        }
31
32
        $parentId = $this->identifyParent($oldestMessage);
33
        $parentEventStream = parent::load($parentId);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (load() instead of loadCompleteStream()). Are you sure this is correct? If so, you might want to change this to $this->load().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
34
35
        $inheritedEvents = $this->limitEventStreamToPlayhead($parentEventStream, $oldestMessage->getPlayhead());
36
        $combinedEvents = array_merge($inheritedEvents, $events);
37
38
        return $this->loadCompleteStream(new DomainEventStream($combinedEvents));
39
    }
40
41
    /**
42
     * @param DomainMessage $message
43
     * @return string
44
     *
45
     * @throws UnknownParentAggregateException
46
     */
47
    private function identifyParent(DomainMessage $message)
48
    {
49
        /** @var AggregateCopiedEventInterface $domainEvent */
50
        $domainEvent = $message->getPayload();
51
52
        if (!$domainEvent instanceof AggregateCopiedEventInterface) {
53
            throw new UnknownParentAggregateException();
54
        }
55
56
        return $domainEvent->getParentAggregateId();
57
    }
58
59
    /**
60
     * @param DomainEventStreamInterface $eventStream
61
     * @param int $playhead
62
     *
63
     * @return DomainMessage[]
64
     */
65
    private function limitEventStreamToPlayhead(DomainEventStreamInterface $eventStream, $playhead)
66
    {
67
        return array_filter(
68
            iterator_to_array($eventStream),
69
            function (DomainMessage $message) use ($playhead) {
70
                return intval($message->getPlayhead()) < $playhead;
71
            }
72
        );
73
    }
74
}
75