DelegateEventHandlingToSpecificMethodTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 9 2
A getHandleMethodName() 0 24 4
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\EventHandling;
7
8
use Broadway\Domain\DomainMessage;
9
10
trait DelegateEventHandlingToSpecificMethodTrait
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15
    public function handle(DomainMessage $domainMessage)
16
    {
17
        $event  = $domainMessage->getPayload();
18
        $method = $this->getHandleMethodName($event);
19
20
        if ($method) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $method of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
21
            $this->$method($event, $domainMessage);
22
        }
23
    }
24
25
    private function getHandleMethodName($event)
26
    {
27
        $classParts = explode('\\', get_class($event));
28
        $methodName = 'apply' . end($classParts);
29
30
        if (!method_exists($this, $methodName)) {
31
            return null;
32
        }
33
34
        try {
35
            $parameter = new \ReflectionParameter(array($this, $methodName), 0);
36
        } catch (\ReflectionException $e) {
37
            // No parameter for the method, so we ignore it.
38
            return null;
39
        }
40
41
        $expectedClass = $parameter->getClass();
42
43
        if ($expectedClass->getName() !== get_class($event)) {
0 ignored issues
show
Bug introduced by
Consider using $expectedClass->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
44
            return null;
45
        }
46
47
        return $methodName;
48
    }
49
}
50