DomainEventDispatcherTrait::handleRecursively()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Event-Sourcing package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\EventSourcing\Entity;
12
13
use Borobudur\Cqrs\Message\DomainEventInterface;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     8/20/15
18
 */
19
trait DomainEventDispatcherTrait
20
{
21
    use EntityNamingTrait;
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function handleRecursively(DomainEventInterface $event)
27
    {
28
        $this->handle($event);
29
30
        foreach ($this->getChildEntities() as $entity) {
31
            if ($this instanceof AggregateRootInterface) {
32
                $entity->registerAggregateRoot($this);
33
            } elseif ($this instanceof EntityInterface) {
34
                $entity->registerAggregateRoot($this->{'aggregateRoot'});
35
            }
36
37
            $entity->handleRecursively($event);
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function handle(DomainEventInterface $event)
45
    {
46
        $method = $this->getApplyMethodName($event);
47
48
        if (!method_exists($this, $method)) {
49
            return;
50
        }
51
52
        $this->{$method}($event);
53
    }
54
55
    /**
56
     * Return child entities.
57
     *
58
     * Override this method if entity has children.
59
     *
60
     * @return EntityInterface[]
61
     */
62
    abstract protected function getChildEntities();
63
}
64