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 Psr\Log\LoggerInterface; |
14
|
|
|
use Assert\Assertion; |
15
|
|
|
use Reith\ToyRobot\Messaging\BusInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractBus implements BusInterface |
18
|
|
|
{ |
19
|
|
|
// Message handlers |
20
|
|
|
private $handlers; |
21
|
|
|
|
22
|
|
|
private $logger; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param LoggerInterface|null $logger |
26
|
|
|
*/ |
27
|
10 |
|
public function __construct(?LoggerInterface $logger = null) |
28
|
|
|
{ |
29
|
10 |
|
$this->logger = $logger; |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param mixed $handler |
34
|
|
|
*/ |
35
|
4 |
|
public function registerHandler($handler): AbstractBus |
36
|
|
|
{ |
37
|
|
|
// Expect the handler to be an object |
38
|
4 |
|
Assertion::isObject($handler); |
39
|
|
|
|
40
|
4 |
|
$this->handlers[] = new HandlerWrapper($handler); |
41
|
|
|
|
42
|
4 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param mixed $message |
47
|
|
|
* |
48
|
|
|
* @throws \Assert\AssertionFailedException |
49
|
|
|
*/ |
50
|
3 |
|
public function post($message, ?callable $callback = null): void |
51
|
|
|
{ |
52
|
|
|
// Expect the message to be an object to use reflection |
53
|
|
|
// and match the handler method |
54
|
3 |
|
Assertion::isObject($message); |
55
|
|
|
|
56
|
3 |
|
$messageHandler = $this->findHandler($message); |
57
|
|
|
|
58
|
3 |
|
$this->log( |
59
|
3 |
|
sprintf( |
60
|
3 |
|
'Sending message [%s] to handler [%s::%s()]', |
61
|
3 |
|
get_class($message), |
62
|
3 |
|
get_class($messageHandler[0]), |
63
|
3 |
|
$messageHandler[1] |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// Run the command |
68
|
3 |
|
$result = call_user_func($messageHandler, $message); |
69
|
|
|
|
70
|
3 |
|
if ($callback) { |
71
|
2 |
|
call_user_func($callback, $result); |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $msg |
77
|
|
|
*/ |
78
|
3 |
|
private function log(string $msg): void |
79
|
|
|
{ |
80
|
3 |
|
if (!$this->logger) { |
81
|
3 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->logger->info($msg); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return callable |
89
|
|
|
* |
90
|
|
|
* @throws \RuntimeException |
91
|
|
|
*/ |
92
|
3 |
|
private function findHandler($message): callable |
93
|
|
|
{ |
94
|
|
|
// In this implementation the first handler wins but |
95
|
|
|
// actually we would want to fail if there is more than |
96
|
|
|
// one for Commands and Queries |
97
|
3 |
|
foreach ($this->handlers as $handler) { |
98
|
3 |
|
if ($callable = $handler->getCallable($message)) { |
99
|
3 |
|
return $callable; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw new \RuntimeException( |
104
|
|
|
sprintf('No handler was found for message [%s]', get_class($message)) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|