UserAggregateRoot::publish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\User\Domain\Model;
14
15
use BenGorUser\User\Domain\Model\Event\UserEvent;
16
17
/**
18
 * User aggregate root class.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
abstract class UserAggregateRoot
23
{
24
    /**
25
     * Array which contains the domain events.
26
     *
27
     * @var array
28
     */
29
    private $events = [];
30
31
    /**
32
     * Clears the events container.
33
     */
34
    public function eraseEvents()
35
    {
36
        $this->events = [];
37
    }
38
39
    /**
40
     * Gets the recorded domain events.
41
     *
42
     * @return array
43
     */
44
    public function events()
45
    {
46
        return $this->events;
47
    }
48
49
    /**
50
     * Publishes the domain event.
51
     *
52
     * If the solution needs a singleton based event system,
53
     * this methods will be overwritten.
54
     *
55
     * The recommend way is to record events domains in the aggregate root
56
     * so, by default, this method calls to the "record" method.
57
     *
58
     * @param UserEvent $anEvent The domain event
59
     */
60
    protected function publish(UserEvent $anEvent)
61
    {
62
        $this->record($anEvent);
63
    }
64
65
    /**
66
     * Saves the given domain event inside event container.
67
     *
68
     * @param UserEvent $anEvent The domain event
69
     */
70
    private function record(UserEvent $anEvent)
71
    {
72
        $this->events[] = $anEvent;
73
    }
74
}
75