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.

AggregateRoot   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 8.42 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 8
loc 95
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 8 8 2
A getApplyMethod() 0 6 1
A getUncommittedEvents() 0 8 1
A getChildren() 0 4 1
A apply() 0 12 1
A handleRecursively() 0 9 2
A initializeState() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
25
     * @param Event $event
26
     */
27 18
    public function apply(Event $event)
28
    {
29 18
        $this->handleRecursively($event);
30
31 18
        $this->playHead++;
32 18
        $this->uncommittedEvents[] = DomainMessage::recordNow(
33 18
            $this->getAggregateRootId(),
34 18
            $this->playHead,
35 18
            new Metadata(array()),
36
            $event
37
        );
38 18
    }
39
40
41
    /**
42
     * Handles event if capable.
43
     *
44
     * @param $event
45
     */
46 24 View Code Duplication
    protected function handle(Event $event)
47
    {
48 24
        $method = $this->getApplyMethod($event);
49 24
        if (!method_exists($this, $method)) {
50 18
            return;
51
        }
52 6
        $this->$method($event);
53 6
    }
54
55
    /**
56
     * @param Event $event
57
     */
58 24
    protected function handleRecursively(Event $event)
59
    {
60 24
        $this->handle($event);
61
62 24
        foreach ($this->getChildren() as $child) {
63 6
            $child->registerAggregateRoot($this);
64 6
            $child->handleRecursively($event);
65
        }
66 24
    }
67
68
    /**
69
     * @param Event $event
70
     * @return string
71
     */
72 24
    private function getApplyMethod(Event $event)
73
    {
74 24
        $classParts = explode('\\', get_class($event));
75
76 24
        return 'apply' . end($classParts);
77
    }
78
79
    /**
80
     * @return DomainEventStream
81
     */
82 15
    public function getUncommittedEvents()
83
    {
84 15
        $stream = new DomainEventStream($this->uncommittedEvents);
85
86 15
        $this->uncommittedEvents = [];
87
88 15
        return $stream;
89
    }
90
91
    /**
92
     * @param DomainEventStream $stream
93
     */
94 6
    public function initializeState(DomainEventStream $stream)
95
    {
96 6
        foreach ($stream as $message) {
97 6
            $this->playHead++;
98 6
            $this->handleRecursively($message->getPayload());
99
        }
100 6
    }
101
102
    /**
103
     * @return EntityInterface[] $entity
104
     */
105 15
    public function getChildren()
106
    {
107 15
        return [];
108
    }
109
}