Passed
Push — master ( 2279c5...1edf79 )
by Mr
06:36
created

EventHandlerTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A applyEvent() 0 3 1
A invokeEventHandler() 0 10 2
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/read-model project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\ReadModel\Projection;
10
11
use Daikon\EventSourcing\Aggregate\Event\DomainEventInterface;
12
use Daikon\Interop\RuntimeException;
13
use ReflectionClass;
14
15
trait EventHandlerTrait
16
{
17
    public function applyEvent(DomainEventInterface $domainEvent): ProjectionInterface
18
    {
19
        return $this->invokeEventHandler($domainEvent);
20
    }
21
22
    private function invokeEventHandler(DomainEventInterface $event): ProjectionInterface
23
    {
24
        $handlerName = (new ReflectionClass($event))->getShortName();
25
        $handlerMethod = 'when'.ucfirst($handlerName);
26
        $projection = clone $this;
27
        $handler = [$projection, $handlerMethod];
28
        if (!is_callable($handler)) {
29
            throw new RuntimeException("Handler '$handlerMethod' is not callable on ".static::class);
30
        }
31
        return call_user_func($handler, $event);
32
    }
33
}
34