AbstractEventSourcedEntity   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 19.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 8
Bugs 3 Features 0
Metric Value
wmc 10
c 8
b 3
f 0
lcom 1
cbo 2
dl 15
loc 77
ccs 20
cts 25
cp 0.8
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A handleRecursively() 15 15 4
A registerAggregateRoot() 0 11 3
getChildEntities() 0 1 ?
handle() 0 1 ?
A apply() 0 11 2
A getAggregateRoot() 0 4 1

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
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * The software is based on the Axon Framework project which is
17
 * licensed under the Apache 2.0 license. For more information on the Axon Framework
18
 * see <http://www.axonframework.org/>.
19
 * 
20
 * This software consists of voluntary contributions made by many individuals
21
 * and is licensed under the MIT license. For more information, see
22
 * <http://www.governor-framework.org/>.
23
 */
24
25
namespace Governor\Framework\EventSourcing;
26
27
use Governor\Framework\Domain\MetaData;
28
use Governor\Framework\Domain\DomainEventMessageInterface;
29
30
/**
31
 * Description of AbstractEventSourcedEntity
32
 *
33
 * @author    "David Kalosi" <[email protected]>
34
 * @license   <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a>
35
 */
36
abstract class AbstractEventSourcedEntity implements EventSourcedEntityInterface
37
{
38
    /**
39
     * @var AbstractEventSourcedAggregateRoot
40
     */
41
    private $aggregateRoot;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1 View Code Duplication
    public function handleRecursively(DomainEventMessageInterface $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...
47
    {
48 1
        $this->handle($event);
49
50 1
        if (null === $childEntities = $this->getChildEntities()) {
51
            return;
52
        }
53
54 1
        foreach ($childEntities as $child) {
55 1
            if (null !== $child) {
56 1
                $child->registerAggregateRoot($this->aggregateRoot);
57 1
                $child->handleRecursively($event);
58 1
            }
59 1
        }
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 4
    public function registerAggregateRoot(AbstractEventSourcedAggregateRoot $aggregateRootToRegister)
66
    {
67 4
        if (null !== $this->aggregateRoot && $this->aggregateRoot !== $aggregateRootToRegister) {
68 1
            throw new \RuntimeException(
69
                "Cannot register new aggregate. "
70
                ."This entity is already part of another aggregate"
71 1
            );
72
        }
73
74 4
        $this->aggregateRoot = $aggregateRootToRegister;
75 4
    }
76
77
    /**
78
     * @return EventSourcedEntityInterface[]
79
     */
80
    abstract protected function getChildEntities();
81
82
    /**
83
     * @param DomainEventMessageInterface $event
84
     * @return mixed
85
     */
86
    abstract protected function handle(DomainEventMessageInterface $event);
87
88
    /**
89
     * @param mixed $event
90
     * @param MetaData $metaData
91
     */
92 1
    public function apply($event, MetaData $metaData = null)
93
    {
94 1
        if (null === $this->aggregateRoot) {
95
            throw new \RuntimeException(
96
                "The aggregate root is unknown. "
97
                ."Is this entity properly registered as the child of an aggregate member?"
98
            );
99
        }
100
101 1
        $this->aggregateRoot->apply($event, $metaData);
102 1
    }
103
104
    /**
105
     * @return AbstractEventSourcedAggregateRoot
106
     */
107
    protected function getAggregateRoot()
108
    {
109
        return $this->aggregateRoot;
110
    }
111
112
}
113