Completed
Push — master ( 20b3d1...2b279c )
by Beñat
12:00
created

AggregateRootCapabilities::record()   A

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
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016-2017 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\SharedKernel\Domain\Model;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 */
17
trait AggregateRootCapabilities
18
{
19
    private $recordedEvents = [];
20
21
    public function recordedEvents()
22
    {
23
        return $this->recordedEvents;
24
    }
25
26
    public function clearEvents()
27
    {
28
        $this->recordedEvents = [];
29
    }
30
31
    protected function publish(DomainEvent $event)
32
    {
33
        $this->apply($event);
34
        $this->record($event);
35
    }
36
37
    protected function apply(DomainEvent $event)
38
    {
39
        $modifier = 'apply' . array_reverse(explode('\\', get_class($event)))[0];
40
        if (!method_exists($this, $modifier)) {
41
            return;
42
        }
43
        $this->$modifier($event);
44
    }
45
46
    private function record(DomainEvent $event)
47
    {
48
        $this->recordedEvents[] = $event;
49
    }
50
}
51