OutgoingLogicalMessageMutationPipelineStep   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 49.09 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 27
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 27 27 4
A getStageContextClass() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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