EventsApplierOnAggregate   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 29
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A applyEventsOnAggregate() 0 6 2
A applyEvent() 0 8 2
A getMethodName() 0 6 1
1
<?php
2
/******************************************************************************
3
 * Copyright (c) 2016 Constantin Galbenu <[email protected]>             *
4
 ******************************************************************************/
5
6
namespace Gica\Cqrs\Event\EventsApplier;
7
8
9
use Gica\Cqrs\Event\EventWithMetaData;
10
11
class EventsApplierOnAggregate
12
{
13
    /**
14
     * @param $aggregate
15
     * @param EventWithMetaData[] $priorEvents
16
     */
17 17
    public function applyEventsOnAggregate($aggregate, $priorEvents)
18
    {
19 17
        foreach ($priorEvents as $event) {
20 14
            $this->applyEvent($aggregate, $event);
21
        }
22 17
    }
23
24 14
    private function applyEvent($aggregate, EventWithMetaData $eventWithMetaData)
25
    {
26 14
        $methodName = self::getMethodName($eventWithMetaData->getEvent());
27
28 14
        if (is_callable([$aggregate, $methodName])) {
29 13
            call_user_func([$aggregate, $methodName], $eventWithMetaData->getEvent(), $eventWithMetaData->getMetaData());
30
        }
31 14
    }
32
33 15
    public static function getMethodName($event)
34
    {
35 15
        $parts = explode('\\', get_class($event));
36
37 15
        return 'apply' . end($parts);
38
    }
39
}