Completed
Push — devel ( e3aed1...a59919 )
by Alex
9s
created

UnicastRouter::route()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
ccs 17
cts 17
cp 1
rs 6.7272
cc 7
eloc 15
nc 16
nop 2
crap 7
1
<?php
2
namespace PSB\Core\Routing;
3
4
5
use PSB\Core\SendOptions;
6
use PSB\Core\Transport\Config\TransportInfrastructure;
7
8
class UnicastRouter implements UnicastRouterInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $localAddress;
14
15
    /**
16
     * @var UnicastRoutingTable
17
     */
18
    private $unicastRoutingTable;
19
20
    /**
21
     * @var TransportInfrastructure
22
     */
23
    private $transportInfrastructure;
24
25
    /**
26
     * @param string                  $localAddress
27
     * @param UnicastRoutingTable     $unicastRoutingTable
28
     * @param TransportInfrastructure $transportInfrastructure
29
     */
30 5
    public function __construct(
31
        $localAddress,
32
        UnicastRoutingTable $unicastRoutingTable,
33
        TransportInfrastructure $transportInfrastructure
34
    ) {
35 5
        $this->localAddress = $localAddress;
36 5
        $this->unicastRoutingTable = $unicastRoutingTable;
37 5
        $this->transportInfrastructure = $transportInfrastructure;
38 5
    }
39
40
    /**
41
     * @param SendOptions $options
42
     * @param string      $messageClass
43
     *
44
     * @return AddressTagInterface[]
45
     */
46 4
    public function route(SendOptions $options, $messageClass)
47
    {
48 4
        $destination = $options->isRoutedToLocalInstance() ? $this->localAddress : null;
49 4
        $destination = $options->getExplicitDestination() ?: $destination;
50
51 4
        if ($destination === null || $destination === '') {
52 1
            $messageTypes = array_merge([$messageClass], array_values(class_implements($messageClass, true)));
53 1
            $endpointNames = $this->unicastRoutingTable->getEndpointNamesFor($messageTypes);
54 1
            $destinations = [];
55 1
            foreach ($endpointNames as $endpointName) {
56 1
                $destinations[] = $this->transportInfrastructure->toTransportAddress($endpointName);
57 1
            }
58 1
        } else {
59 3
            $destinations = [$destination];
60
        }
61
62 4
        $addressTags = [];
63 4
        foreach ($destinations as $destination) {
64 4
            $addressTags[] = new UnicastAddressTag($destination);
65 4
        }
66
67 4
        return $addressTags;
68
    }
69
}
70