Passed
Push — master ( 55bb68...dc273d )
by Tilita
06:12 queued 02:39
created

ContainerBuilder::createExchanges()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 27
loc 27
ccs 14
cts 14
cp 1
rs 9.488
c 0
b 0
f 0
cc 4
nc 3
nop 2
crap 4
1
<?php
2
namespace NeedleProject\LaravelRabbitMq\Builder;
3
4
use Illuminate\Support\Collection;
5
use NeedleProject\LaravelRabbitMq\AMQPConnection;
6
use NeedleProject\LaravelRabbitMq\Container;
7
use NeedleProject\LaravelRabbitMq\Entity\ExchangeEntity;
8
use NeedleProject\LaravelRabbitMq\Entity\QueueEntity;
9
10
/**
11
 * Class ContainerBuilder
12
 *
13
 * @package NeedleProject\LaravelRabbitMq\Builder
14
 * @author  Adrian Tilita <[email protected]>
15
 * @todo    Add config validator
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
16
 */
17
class ContainerBuilder
18
{
19
    /**
20
     * Create RabbitMQ Container
21
     *
22
     * @param array $config
23
     * @return Container
24
     */
25 7
    public function createContainer(array $config)
26
    {
27 7
        $connections = $this->createConnections($config['connections']);
28 7
        $exchanges = $this->createExchanges($config['exchanges'], $connections);
29 6
        $queues = $this->createQueues($config['queues'], $connections);
30
31 5
        $container = new Container();
32
        // create publishers
33 5
        foreach ($config['publishers'] as $publisherAliasName => $publisherEntityBind) {
34 3
            if ($exchanges->has($publisherEntityBind)) {
35 1
                $entity = $exchanges->get($publisherEntityBind);
36 2
            } elseif ($queues->has($publisherEntityBind)) {
37 1
                $entity = $queues->get($publisherEntityBind);
38
            } else {
39 1
                throw new \RuntimeException(
40
                    sprintf(
41 1
                        "Cannot create publisher %s: no exchange or queue named %s defined!",
42 1
                        (string)$publisherAliasName,
43 1
                        (string)$publisherEntityBind
44
                    )
45
                );
46
            }
47
48 2
            $container->addPublisher(
49
                $publisherAliasName,
50
                $entity
51
            );
52
        }
53
54 4
        foreach ($config['consumers'] as $consumerAliasName => $consumerDetails) {
55 2
            $prefetchCount    = $consumerDetails['prefetch_count'];
56 2
            $messageProcessor = $consumerDetails['message_processor'];
57
58 2
            if ($queues->has($consumerDetails['queue'])) {
59
                /** @var QueueEntity $entity */
60 1
                $entity = $queues->get($consumerDetails['queue']);
61
            } else {
62 1
                throw new \RuntimeException(
63
                    sprintf(
64 1
                        "Cannot create consumer %s: no queue named %s defined!",
65 1
                        (string)$consumerAliasName,
66 1
                        (string)$consumerDetails['queue']
67
                    )
68
                );
69
            }
70
71 1
            $entity->setPrefetchCount($prefetchCount);
72 1
            $entity->setMessageProcessor($messageProcessor);
73 1
            $container->addConsumer($consumerAliasName, $entity);
74
        }
75
76 3
        return $container;
77
    }
78
79
    /**
80
     * Create connections
81
     *
82
     * @param array $connectionConfig
83
     * @return Collection
84
     */
85 7
    private function createConnections(array $connectionConfig): Collection
86
    {
87 7
        $connections = new Collection();
88 7
        foreach ($connectionConfig as $connectionAliasName => $connectionCredentials) {
89 7
            $connections->put(
90
                $connectionAliasName,
91 7
                AMQPConnection::createConnection($connectionAliasName, $connectionCredentials)
92
            );
93
        }
94 7
        return $connections;
95
    }
96
97
    /**
98
     * @param array $exchangeConfigList
99
     * @param Collection $connections
100
     * @return Collection
101
     */
102 7 View Code Duplication
    private function createExchanges(array $exchangeConfigList, Collection $connections): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104 7
        $exchanges = new Collection();
105 7
        foreach ($exchangeConfigList as $exchangeAliasName => $exchangeDetails) {
106
            // verify if the connection exists
107 3
            if (array_key_exists('connection', $exchangeDetails) &&
108 3
                false === $connections->has($exchangeDetails['connection'])) {
109 1
                throw new \RuntimeException(
110
                    sprintf(
111 1
                        "Could not create exchange %s: connection name %s is not defined!",
112 1
                        (string)$exchangeAliasName,
113 1
                        (string)$exchangeDetails['connection']
114
                    )
115
                );
116
            }
117
118 2
            $exchanges->put(
119
                $exchangeAliasName,
120 2
                ExchangeEntity::createExchange(
121 2
                    $connections->get($exchangeDetails['connection']),
122
                    $exchangeAliasName,
123 2
                    array_merge($exchangeDetails['attributes'], ['name' => $exchangeDetails['name']])
124
                )
125
            );
126
        }
127 6
        return $exchanges;
128
    }
129
130
    /**
131
     * @param array $queueConfigList
132
     * @param Collection $connections
133
     * @return Collection
134
     */
135 6 View Code Duplication
    private function createQueues(array $queueConfigList, Collection $connections): Collection
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137 6
        $queue = new Collection();
138 6
        foreach ($queueConfigList as $queueAliasName => $queueDetails) {
139
            // verify if the connection exists
140 4
            if (array_key_exists('connection', $queueDetails) &&
141 4
                false === $connections->has($queueDetails['connection'])) {
142 1
                throw new \RuntimeException(
143
                    sprintf(
144 1
                        "Could not create exchange %s: connection name %s is not defined!",
145 1
                        (string)$queueAliasName,
146 1
                        (string)$queueDetails['connection']
147
                    )
148
                );
149
            }
150
151 3
            $queue->put(
152
                $queueAliasName,
153 3
                QueueEntity::createQueue(
154 3
                    $connections->get($queueDetails['connection']),
155
                    $queueAliasName,
156 3
                    array_merge($queueDetails['attributes'], ['name' => $queueDetails['name']])
157
                )
158
            );
159
        }
160 5
        return $queue;
161
    }
162
}
163