UnicastSendRoutingConnector::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\OutgoingSendContext;
11
use PSB\Core\Pipeline\StageConnectorInterface;
12
use PSB\Core\Routing\UnicastRouterInterface;
13
14
class UnicastSendRoutingConnector implements StageConnectorInterface
15
{
16
    /**
17
     * @var UnicastRouterInterface
18
     */
19
    private $unicastRouter;
20
21
    /**
22
     * @var OutgoingContextFactory
23
     */
24
    private $contextFactory;
25
26
    /**
27
     * @param UnicastRouterInterface $unicastRouter
28
     * @param OutgoingContextFactory $contextFactory
29
     */
30 6
    public function __construct(
31
        UnicastRouterInterface $unicastRouter,
32
        OutgoingContextFactory $contextFactory
33
    ) {
34 6
        $this->unicastRouter = $unicastRouter;
35 6
        $this->contextFactory = $contextFactory;
36 6
    }
37
38
    /**
39
     * @param OutgoingSendContext $context
40
     * @param callable            $next
41
     */
42 3
    public function invoke($context, callable $next)
43
    {
44 3
        $addressTags = $this->unicastRouter->route($context->getSendOptions(), $context->getMessageClass());
45
46 3
        if (empty($addressTags)) {
47 1
            throw new RoutingException(
48 1
                "The message destination could not be determined. You may have misconfigured the destination for this kind of message ({$context->getMessageClass()}) when you registered the message to endpoint mappings."
49
            );
50
        }
51
52 2
        $context->setHeader(HeaderTypeEnum::MESSAGE_INTENT, MessageIntentEnum::SEND);
53
54 2
        $logicalMessageContext = $this->contextFactory->createLogicalMessageContextFromSendContext(
55 2
            $addressTags,
56 2
            $context
57
        );
58
59 2
        $next($logicalMessageContext);
60 2
    }
61
62
    /**
63
     * @return string
64
     */
65 1
    public static function getStageContextClass()
66
    {
67 1
        return OutgoingSendContext::class;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 1
    public static function getNextStageContextClass()
74
    {
75 1
        return OutgoingLogicalMessageContext::class;
76
    }
77
}
78