IncomingLogicalMessageMutationPipelineStep   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 43.55 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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\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
}