|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSB\Core\MessageMutation\Pipeline\Outgoing; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use PSB\Core\MessageMutatorRegistry; |
|
6
|
|
|
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingLogicalMessageContext; |
|
7
|
|
|
use PSB\Core\Pipeline\PipelineStepInterface; |
|
8
|
|
|
|
|
9
|
|
|
class OutgoingLogicalMessageMutationPipelineStep implements PipelineStepInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var MessageMutatorRegistry |
|
13
|
|
|
*/ |
|
14
|
|
|
private $mutatorRegistry; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param MessageMutatorRegistry $mutatorRegistry |
|
18
|
|
|
*/ |
|
19
|
5 |
|
public function __construct(MessageMutatorRegistry $mutatorRegistry) |
|
20
|
|
|
{ |
|
21
|
5 |
|
$this->mutatorRegistry = $mutatorRegistry; |
|
22
|
5 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param OutgoingLogicalMessageContext $context |
|
26
|
|
|
* @param callable $next |
|
27
|
|
|
*/ |
|
28
|
3 |
View Code Duplication |
public function invoke($context, callable $next) |
|
29
|
|
|
{ |
|
30
|
3 |
|
$mutatorIds = $this->mutatorRegistry->getOutgoingLogicalMessageMutatorIds(); |
|
31
|
|
|
|
|
32
|
3 |
|
if (empty($mutatorIds)) { |
|
33
|
1 |
|
$next(); |
|
34
|
1 |
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
$logicalMessage = $context->getMessage(); |
|
38
|
2 |
|
$messageInstance = $logicalMessage->getMessageInstance(); |
|
39
|
|
|
|
|
40
|
2 |
|
$mutatorContext = new OutgoingLogicalMessageMutationContext($messageInstance, $context->getHeaders()); |
|
41
|
|
|
|
|
42
|
2 |
|
foreach ($mutatorIds as $mutatorId) { |
|
43
|
|
|
/** @var OutgoingLogicalMessageMutatorInterface $mutator */ |
|
44
|
2 |
|
$mutator = $context->getBuilder()->build($mutatorId); |
|
45
|
2 |
|
$mutator->mutateOutgoing($mutatorContext); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
if ($mutatorContext->hasMessageChanged()) { |
|
49
|
1 |
|
$logicalMessage->updateInstance($mutatorContext->getMessage()); |
|
50
|
|
|
} |
|
51
|
2 |
|
$context->replaceHeaders($mutatorContext->getHeaders()); |
|
52
|
|
|
|
|
53
|
2 |
|
$next(); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function getStageContextClass() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return OutgoingLogicalMessageContext::class; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|