UnicastReplyRoutingConnector::__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\Routing\Pipeline;
3
4
5
use PSB\Core\Exception\RoutingException;
6
use PSB\Core\HeaderTypeEnum;
7
use PSB\Core\MessageIntentEnum;
8
use PSB\Core\Pipeline\Outgoing\OutgoingContextFactory;
9
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingLogicalMessageContext;
10
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingReplyContext;
11
use PSB\Core\Pipeline\StageConnectorInterface;
12
use PSB\Core\Transport\IncomingPhysicalMessage;
13
14
class UnicastReplyRoutingConnector implements StageConnectorInterface
15
{
16
    /**
17
     * @var OutgoingContextFactory
18
     */
19
    private $contextFactory;
20
21
    /**
22
     * @param OutgoingContextFactory $contextFactory
23
     */
24 9
    public function __construct(OutgoingContextFactory $contextFactory)
25
    {
26 9
        $this->contextFactory = $contextFactory;
27 9
    }
28
29
    /**
30
     * @param OutgoingReplyContext $context
31
     * @param callable             $next
32
     */
33 6
    public function invoke($context, callable $next)
34
    {
35 6
        $replyToAddress = $context->getReplyOptions()->getExplicitDestination();
36 6
        if (!$replyToAddress) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $replyToAddress of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37 5
            $replyToAddress = $this->getReplyToAddressFromIncomingMessage($context->getIncomingPhysicalMessage());
38
        }
39
40 4
        $context->setHeader(HeaderTypeEnum::MESSAGE_INTENT, MessageIntentEnum::REPLY);
41
42 4
        $logicalMessageContext = $this->contextFactory->createLogicalMessageContextFromReplyContext(
43 4
            $replyToAddress,
44 4
            $context
45
        );
46
47 4
        $next($logicalMessageContext);
48 4
    }
49
50
    /**
51
     * @param IncomingPhysicalMessage|null $incomingPhysicalMessage
52
     *
53
     * @return string
54
     */
55 5
    private function getReplyToAddressFromIncomingMessage(IncomingPhysicalMessage $incomingPhysicalMessage = null)
56
    {
57 5
        if (!$incomingPhysicalMessage) {
58 1
            throw new RoutingException(
59 1
                "No incoming message found, replies are only valid to call from a message handler."
60
            );
61
        }
62
63 4
        $replyToAddress = $incomingPhysicalMessage->getReplyToAddress();
64 4
        if ($replyToAddress === null || $replyToAddress === '') {
65 1
            $messageType = $incomingPhysicalMessage->getHeaders()[HeaderTypeEnum::ENCLOSED_CLASS];
66 1
            throw new RoutingException("No 'ReplyToAddress' found on the '$messageType' being processed");
67
        }
68
69 3
        return $incomingPhysicalMessage->getReplyToAddress();
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public static function getStageContextClass()
76
    {
77 1
        return OutgoingReplyContext::class;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 1
    public static function getNextStageContextClass()
84
    {
85 1
        return OutgoingLogicalMessageContext::class;
86
    }
87
}
88