Completed
Push — master ( a4b4a5...98881f )
by Douglas
02:14
created

HandlerWrapper::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 29
cts 29
cp 1
rs 9.125
c 0
b 0
f 0
cc 3
eloc 28
nc 3
nop 1
crap 3
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\Annotation\Subscribe;
16
17
class HandlerWrapper
18
{
19
    private $handler;
20
21
    private $callables;
22
23
    /**
24
     * Part of this inspired by `AnnotatedMessageHandlerDescriptor`
25
     * in https://github.com/szjani/predaddy.
26
     *
27
     * @param mixed $handler
28
     *
29
     * @throws \Assert\AssertionFailedException
30
     */
31 9
    public function __construct($handler)
32
    {
33 9
        Assertion::isObject($handler, 'Expected an object for a handler');
34
35 9
        $reflection = new \ReflectionClass($handler);
36 9
        $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
37
38 9
        Assertion::greaterThan(
39 9
            count($methods),
40 9
            0,
41 9
            sprintf('No methods found on handler [%s]', get_class($handler))
42
        );
43
44 8
        $reader = new AnnotationReader();
45
46 8
        foreach ($methods as $method) {
47 8
            $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 8
            if (!$methodAnnotation) {
50 8
                continue;
51
            }
52
53 8
            $params = $method->getParameters();
54 8
            Assertion::eq(
55 8
                count($params),
56 8
                1,
57 8
                sprintf(
58 8
                    'Expect only one argument for a handler method [%s::%s()]',
59 8
                    get_class($handler),
60 8
                    $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 8
            $param = $params[0];
65
66 8
            $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 8
            $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 8
        Assertion::greaterThan(
74 8
            count($this->callables),
75 8
            0,
76 8
            sprintf('No handler methods found on [%s]', get_class($handler))
77
        );
78 8
    }
79
80
    /**
81
     * @param mixed $message
82
     *
83
     * @return callable
84
     */
85 7
    public function getCallable($message): ?callable
86
    {
87 7
        Assertion::isObject($message, 'Expected an object for a message');
88
89 7
        if (isset($this->callables[get_class($message)])) {
90 7
            return $this->callables[get_class($message)];
91
        }
92
93 3
        return null;
94
    }
95
}
96