AttachCorrelationIdPipelineStep::invoke()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9137
c 0
b 0
f 0
cc 6
nc 10
nop 2
crap 6
1
<?php
2
namespace PSB\Core\Correlation\Pipeline;
3
4
5
use PSB\Core\HeaderTypeEnum;
6
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingLogicalMessageContext;
7
use PSB\Core\Pipeline\PipelineStepInterface;
8
9
class AttachCorrelationIdPipelineStep implements PipelineStepInterface
10
{
11
    /**
12
     * @param OutgoingLogicalMessageContext $context
13
     * @param callable                      $next
14
     */
15 3
    public function invoke($context, callable $next)
16
    {
17 3
        $correlationId = null;
18
19 3
        if ($context->getIncomingPhysicalMessage()) {
20 2
            $incomingHeaders = $context->getIncomingPhysicalMessage()->getHeaders();
21
22 2
            if (isset($incomingHeaders[HeaderTypeEnum::CORRELATION_ID])) {
23 1
                $correlationId = $incomingHeaders[HeaderTypeEnum::CORRELATION_ID];
24
            }
25
26 2
            if (!$correlationId && isset($incomingHeaders[HeaderTypeEnum::MESSAGE_ID])) {
27 1
                $correlationId = $incomingHeaders[HeaderTypeEnum::MESSAGE_ID];
28
            }
29
        }
30
31 3
        if (!$correlationId) {
32 1
            $correlationId = $context->getMessageId();
33
        }
34
35 3
        $context->setHeader(HeaderTypeEnum::CORRELATION_ID, $correlationId);
36
37 3
        $next();
38 3
    }
39
40
    /**
41
     * @return string
42
     */
43 1
    public static function getStageContextClass()
44
    {
45 1
        return OutgoingLogicalMessageContext::class;
46
    }
47
}
48