GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#22)
by jrdn
11:31 queued 08:22
created

AggregateRoot::addChildEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace SmoothPhp\EventSourcing;
3
4
use SmoothPhp\Contracts\EventSourcing\AggregateRoot as AggregateRootInterface;
5
use SmoothPhp\Contracts\EventSourcing\Entity as EntityInterface;
6
use SmoothPhp\Contracts\EventSourcing\Event;
7
use SmoothPhp\Domain\DomainEventStream;
8
use SmoothPhp\Domain\DomainMessage;
9
use SmoothPhp\Domain\Metadata;
10
11
/**
12
 * Class AggregateRoot
13
 * @author Simon Bennett <[email protected]>
14
 */
15
abstract class AggregateRoot implements AggregateRootInterface
16
{
17
    /**
18
     * @var []
19
     */
20
    private $uncommittedEvents = [];
21
22
    private $playHead = -1;
23
24
    /** @var EntityInterface[] */
25
    private $children = [];
26
27
    /**
28
     * @param Event $event
29
     */
30 18
    public function apply(Event $event)
31
    {
32 18
        $this->handleRecursively($event);
33
34 18
        $this->playHead++;
35 18
        $this->uncommittedEvents[] = DomainMessage::recordNow(
36 18
            $this->getAggregateRootId(),
37 18
            $this->playHead,
38 18
            new Metadata(array()),
39
            $event
40 18
        );
41 18
    }
42
43
44
    /**
45
     * Handles event if capable.
46
     *
47
     * @param $event
48
     */
49 24 View Code Duplication
    protected function handle(Event $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 24
        $method = $this->getApplyMethod($event);
52 24
        if (!method_exists($this, $method)) {
53 18
            return;
54
        }
55 6
        $this->$method($event);
56 6
    }
57
58
    /**
59
     * @param Event $event
60
     */
61 18
    protected function handleRecursively(Event $event)
62
    {
63 18
        $this->handle($event);
64
65 18
        foreach ($this->children as $child) {
66 6
            $child->registerAggregateRoot($this);
67 6
            $child->handleRecursively($event);
68 18
        }
69 18
    }
70
71
    /**
72
     * @param Event $event
73
     * @return string
74
     */
75 24
    private function getApplyMethod(Event $event)
76
    {
77 24
        $classParts = explode('\\', get_class($event));
78
79 24
        return 'apply' . end($classParts);
80
    }
81
82
    /**
83
     * @return DomainEventStream
84
     */
85 15
    public function getUncommittedEvents()
86
    {
87 15
        $stream = new DomainEventStream($this->uncommittedEvents);
88
89 15
        $this->uncommittedEvents = [];
90
91 15
        return $stream;
92
    }
93
94
    /**
95
     * @param DomainEventStream $stream
96
     */
97 6
    public function initializeState(DomainEventStream $stream)
98
    {
99 6
        foreach ($stream as $message) {
100 6
            $this->playHead++;
101 6
            $this->handle($message->getPayload());
102 6
        }
103 6
    }
104
105
    /**
106
     * @param EntityInterface $entity
107
     */
108 6
    public function addChildEntity(EntityInterface $entity)
109
    {
110 6
        $this->children[] = $entity;
111
    }
112
}