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