AbstractEventSourcedEntity::handleRecursively()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 4.012

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 15
ccs 10
cts 11
cp 0.9091
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4.012
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