EventsApplierOnAggregate::applyEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
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
}