AbstractEntity::terminate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\Bus\Message\MessageMapperTrait;
14
use Borobudur\Cqrs\Message\DomainEventInterface;
15
use Borobudur\EventSourcing\Exception\InvalidArgumentException;
16
use Borobudur\EventSourcing\Exception\LogicException;
17
use Borobudur\Serialization\Serializer\Mixin\DeserializerTrait;
18
use Borobudur\Serialization\Serializer\Mixin\SerializerTrait;
19
20
/**
21
 * @author      Iqbal Maulana <[email protected]>
22
 * @created     8/20/15
23
 */
24
abstract class AbstractEntity implements EntityInterface
25
{
26
    use SerializerTrait, DeserializerTrait, DomainEventDispatcherTrait, StateTrait, MessageMapperTrait;
27
28
    /**
29
     * @var AggregateRootInterface
30
     */
31
    protected $aggregateRoot;
32
33
    /**
34
     * @var bool
35
     */
36
    private $terminated = false;
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function registerAggregateRoot(AggregateRootInterface $aggregateRoot)
42
    {
43
        if (null !== $this->aggregateRoot && $this->aggregateRoot !== $aggregateRoot) {
44
            throw new InvalidArgumentException(sprintf(
45
                'Aggregate root "%s" is already registered in "%s".',
46
                get_class($aggregateRoot),
47
                get_class($this)
48
            ));
49
        }
50
51
        $this->aggregateRoot = $aggregateRoot;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function apply(DomainEventInterface $event)
58
    {
59
        if (true === $this->terminated) {
60
            throw new LogicException(sprintf('"%s" has been deleted.', get_called_class()));
61
        }
62
63
        $this->aggregateRoot->apply($event);
64
    }
65
66
    /**
67
     * Get aggregate root.
68
     *
69
     * @return AggregateRootInterface
70
     */
71
    public function getAggregateRoot()
72
    {
73
        return $this->aggregateRoot;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected function terminate()
80
    {
81
        $this->terminated = true;
82
    }
83
}
84