Completed
Push — master ( c54dd1...7d6afc )
by Douglas
03:28
created

HandlerWrapper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.06%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 77
ccs 33
cts 34
cp 0.9706
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 48 3
A getCallable() 0 10 2
1
<?php
2
3
/**
4
 * (c) 2018 Douglas Reith.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace Reith\ToyRobot\Infrastructure\Bus;
12
13
use Assert\Assertion;
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use Reith\ToyRobot\Messaging\BusInterface;
16
use Reith\ToyRobot\Messaging\Annotation\Subscribe;
17
18
class HandlerWrapper
19
{
20
    private $handler;
21
22
    private $callables;
23
24
    /**
25
     * Part of this inspired by `AnnotatedMessageHandlerDescriptor`
26
     * in https://github.com/szjani/predaddy
27
     *
28
     * @param mixed $handler
29
     * @throws \Assert\AssertionFailedException
30
     */
31 2
    public function __construct($handler)
32
    {
33 2
        Assertion::isObject($handler, 'Expected an object for a handler');
34
35 2
        $reflection = new \ReflectionClass($handler);
36 2
        $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
37
38 2
        Assertion::greaterThan(
39 2
            count($methods),
40 2
            0,
41 2
            sprintf('No methods found on handler [%s]', get_class($handler))
42
        );
43
44 1
        $reader = new AnnotationReader();
45
46 1
        foreach ($methods as $method) {
47 1
            $methodAnnotation = $reader->getMethodAnnotation($method, Subscribe::class);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $methodAnnotation is correct as $reader->getMethodAnnota...ation\Subscribe::class) (which targets Doctrine\Common\Annotati...::getMethodAnnotation()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
48
49 1
            if (!$methodAnnotation) {
50 1
                continue;
51
            }
52
53 1
            $params = $method->getParameters();
54 1
            Assertion::eq(
55 1
                count($params),
56 1
                1,
57 1
                sprintf(
58 1
                    'Expect only one argument for a handler method [%s::%s()]',
59 1
                    get_class($handler),
60 1
                    $method->getName()
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
61
                )
62
            );
63
64 1
            $param = $params[0];
65
66 1
            $paramClass = $param->getClass();
67
68
            // Create an index of message class mapped to a callable:
69
            // [$obj, 'methodName']
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
70 1
            $this->callables[$paramClass->getName()] = [$handler, $method->getName()];
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
71
        }
72
73 1
        Assertion::greaterThan(
74 1
            count($this->callables),
75 1
            0,
76 1
            sprintf('No handler methods found on [%s]', get_class($handler))
77
        );
78 1
    }
79
80
    /**
81
     * @param mixed $message
82
     * @return callable
83
     */
84 1
    public function getCallable($message): ?callable
85
    {
86 1
        Assertion::isObject($message, 'Expected an object for a message');
87
88 1
        if (isset($this->callables[get_class($message)])) {
89 1
            return $this->callables[get_class($message)];
90
        }
91
92
        return null;
93
    }
94
}
95