__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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