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

AggregateRootCapabilities   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 34
c 1
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A recordedEvents() 0 4 1
A clearEvents() 0 4 1
A publish() 0 5 1
A apply() 0 8 2
A record() 0 4 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