TransportOperationsConverter::__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\Pipeline\Incoming;
3
4
5
use PSB\Core\Exception\InvalidArgumentException;
6
use PSB\Core\Outbox\OutboxMessage;
7
use PSB\Core\Outbox\OutboxTransportOperation;
8
use PSB\Core\Outbox\OutboxTransportOperationFactory;
9
use PSB\Core\Pipeline\PendingTransportOperations;
10
use PSB\Core\Routing\AddressTagInterface;
11
use PSB\Core\Routing\MulticastAddressTag;
12
use PSB\Core\Routing\UnicastAddressTag;
13
use PSB\Core\Transport\OutgoingPhysicalMessage;
14
use PSB\Core\Transport\TransportOperation;
15
16
class TransportOperationsConverter
17
{
18
    /**
19
     * @var OutboxTransportOperationFactory
20
     */
21
    private $outboxOperationFactory;
22
23
    /**
24
     * @param OutboxTransportOperationFactory $outboxOperationFactory
25
     */
26 5
    public function __construct(OutboxTransportOperationFactory $outboxOperationFactory)
27
    {
28 5
        $this->outboxOperationFactory = $outboxOperationFactory;
29 5
    }
30
31
    /**
32
     * @param PendingTransportOperations $transportOperations
33
     *
34
     * @return OutboxTransportOperation[]
35
     */
36 2
    public function convertToOutboxOperations(PendingTransportOperations $transportOperations)
37
    {
38 2
        $outboxOperations = [];
39 2
        foreach ($transportOperations->getOperations() as $operation) {
40 2
            $outboxOperations[] = $this->outboxOperationFactory->create(
41 2
                $operation->getMessage(),
42 2
                $this->serializeAddressTag($operation->getAddressTag())
43
            );
44
        }
45
46 1
        return $outboxOperations;
47
    }
48
49
    /**
50
     * @param AddressTagInterface $addressTag
51
     *
52
     * @return array
53
     */
54 2
    private function serializeAddressTag(AddressTagInterface $addressTag)
55
    {
56 2
        if ($addressTag instanceof UnicastAddressTag) {
57 1
            return ['destination' => $addressTag->getDestination()];
58
        }
59
60 2
        if ($addressTag instanceof MulticastAddressTag) {
61 1
            return ['message_type' => $addressTag->getMessageType()];
62
        }
63
64 1
        throw new InvalidArgumentException("Unknown address tag type :'" . get_class($addressTag) . "'.'");
65
    }
66
67
    /**
68
     * @param OutboxMessage $outboxMessage
69
     *
70
     * @return PendingTransportOperations
71
     */
72 2
    public function convertToPendingTransportOperations(OutboxMessage $outboxMessage)
73
    {
74 2
        $transportOperations = new PendingTransportOperations();
75 2
        foreach ($outboxMessage->getTransportOperations() as $outboxOperation) {
76 2
            $transportOperations->add(
77 2
                new TransportOperation(
78 2
                    new OutgoingPhysicalMessage(
79 2
                        $outboxOperation->getMessageId(),
80 2
                        $outboxOperation->getHeaders(),
81 2
                        $outboxOperation->getBody()
82
                    ),
83 2
                    $this->deserializeAddressTag($outboxOperation->getOptions())
84
                )
85
            );
86
        }
87
88 1
        return $transportOperations;
89
    }
90
91
    /**
92
     * @param array $tagAsArray
93
     *
94
     * @return AddressTagInterface
95
     */
96 2
    private function deserializeAddressTag(array $tagAsArray)
97
    {
98 2
        if (isset($tagAsArray['destination'])) {
99 1
            return new UnicastAddressTag($tagAsArray['destination']);
100
        }
101
102 2
        if (isset($tagAsArray['message_type'])) {
103 1
            return new MulticastAddressTag($tagAsArray['message_type']);
104
        }
105
106 1
        throw new InvalidArgumentException("Could not find address tag type to deserialize.");
107
    }
108
}
109