TriggerEvents   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
id() 0 1 ?
A recordedEvents() 0 4 1
A clearEvents() 0 4 1
A publish() 0 12 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-present 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
declare(strict_types=1);
13
14
namespace LIN3S\SharedKernel\Domain\Model;
15
16
use LIN3S\SharedKernel\Domain\Event\DomainEventPublisher;
17
18
/**
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
trait TriggerEvents
22
{
23
    private $recordedEvents = [];
24
25
    abstract public function id();
26
27
    public function recordedEvents() : array
28
    {
29
        return $this->recordedEvents;
30
    }
31
32
    public function clearEvents() : void
33
    {
34
        $this->recordedEvents = [];
35
    }
36
37
    protected function publish(DomainEvent $event) : void
38
    {
39
        $this->apply($event);
40
        $this->record($event);
41
        DomainEventPublisher::instance()->publish(
42
            new PublishableDomainEvent(
43
                $this->id(),
44
                mb_strtolower(array_reverse(explode('\\', get_class($this)))[0]),
45
                $event
46
            )
47
        );
48
    }
49
50
    protected function apply(DomainEvent $event) : void
51
    {
52
        $modifier = 'apply' . array_reverse(explode('\\', get_class($event)))[0];
53
        if (!method_exists($this, $modifier)) {
54
            return;
55
        }
56
        $this->$modifier($event);
57
    }
58
59
    private function record(DomainEvent $event) : void
60
    {
61
        $this->recordedEvents[] = $event;
62
    }
63
}
64