1
|
|
|
<?php |
2
|
|
|
namespace PSB\Core\Pipeline\Incoming; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use PSB\Core\Exception\UnexpectedValueException; |
6
|
|
|
use PSB\Core\MessageHandlerInterface; |
7
|
|
|
use PSB\Core\MessageHandlerRegistry; |
8
|
|
|
use PSB\Core\Pipeline\Incoming\StageContext\IncomingLogicalMessageContext; |
9
|
|
|
use PSB\Core\Pipeline\Incoming\StageContext\InvokeHandlerContext; |
10
|
|
|
use PSB\Core\Pipeline\StageConnectorInterface; |
11
|
|
|
|
12
|
|
|
class LoadHandlersConnector implements StageConnectorInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var MessageHandlerRegistry |
17
|
|
|
*/ |
18
|
|
|
private $messageHandlerRegistry; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var IncomingContextFactory |
22
|
|
|
*/ |
23
|
|
|
private $incomingContextFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param MessageHandlerRegistry $messageHandlerRegistry |
27
|
|
|
* @param IncomingContextFactory $incomingContextFactory |
28
|
|
|
*/ |
29
|
7 |
|
public function __construct( |
30
|
|
|
MessageHandlerRegistry $messageHandlerRegistry, |
31
|
|
|
IncomingContextFactory $incomingContextFactory |
32
|
|
|
) { |
33
|
7 |
|
$this->messageHandlerRegistry = $messageHandlerRegistry; |
34
|
7 |
|
$this->incomingContextFactory = $incomingContextFactory; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param IncomingLogicalMessageContext $context |
39
|
|
|
* @param callable $next |
40
|
|
|
*/ |
41
|
4 |
|
public function invoke($context, callable $next) |
42
|
|
|
{ |
43
|
4 |
|
$messageTypes = $context->getMessage()->getMessageTypes(); |
44
|
4 |
|
$handlerIdsToInvoke = $this->messageHandlerRegistry->getHandlerIdsFor($messageTypes); |
45
|
|
|
|
46
|
4 |
|
if (!$context->isMessageHandled() && !count($handlerIdsToInvoke)) { |
47
|
1 |
|
$messageTypes = implode(',', $messageTypes); |
48
|
1 |
|
throw new UnexpectedValueException("No message handlers could be found for message types '$messageTypes'."); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
foreach ($handlerIdsToInvoke as $handlerId) { |
52
|
|
|
/** @var MessageHandlerInterface $handler */ |
53
|
2 |
|
$handler = $context->getBuilder()->build($handlerId); |
54
|
2 |
|
$handlingContext = $this->incomingContextFactory->createInvokeHandlerContext($handler, $context); |
55
|
2 |
|
$next($handlingContext); |
56
|
|
|
|
57
|
2 |
|
if ($handlingContext->isHandlerInvocationAborted()) { |
58
|
|
|
//if the chain was aborted skip the other handlers |
59
|
1 |
|
break; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
$context->markMessageAsHandled(); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public static function getStageContextClass() |
70
|
|
|
{ |
71
|
1 |
|
return IncomingLogicalMessageContext::class; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
public static function getNextStageContextClass() |
78
|
|
|
{ |
79
|
1 |
|
return InvokeHandlerContext::class; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|