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
04:46 queued 02:35
created

AggregateRoot::initializeState()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace SmoothPhp\EventSourcing;
3
4
use SmoothPhp\Contracts\EventSourcing\AggregateRoot as AggregateRootInterface;
5
use SmoothPhp\Contracts\EventSourcing\Entity;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SmoothPhp\EventSourcing\Entity.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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 Entity[] */
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 Entity $entity
107
     */
108 6
    public function addChildEntity(Entity $entity)
109
    {
110 6
        $this->children[] = $entity;
111
    }
112
}