AggregateAppliesKnownEvents   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 6
c 2
b 0
f 1
dl 0
loc 14
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A apply() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
use function method_exists;
8
9
/**
10
 * This trait calls the apply[EventClassName] method
11
 * only for the events it has an apply function for.
12
 * Use this trait when you have many events that you
13
 * do not have an apply method for. This is common
14
 * for when you record many events for analytics.
15
 */
16
trait AggregateAppliesKnownEvents
17
{
18
    private int $aggregateRootVersion = 0;
19
20
    public function apply(object $event): void
21
    {
22
        $parts = explode('\\', get_class($event));
23
        $methodName = 'apply' . end($parts);
24
25
        if (method_exists($this, $methodName)) {
26
            $this->{$methodName}($event);
27
        }
28
29
        ++$this->aggregateRootVersion;
30
    }
31
}
32