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); |
|
|
|
|
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() |
|
|
|
|
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'] |
|
|
|
|
70
|
1 |
|
$this->callables[$paramClass->getName()] = [$handler, $method->getName()]; |
|
|
|
|
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
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
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.