Completed
Push — master ( 57dab7...a3bee9 )
by Tomáš
05:06
created

BunnyManager::getChannel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Portiny\RabbitMQ;
4
5
use Bunny\Async\Client as AsyncClient;
6
use Bunny\Channel;
7
use Bunny\Client;
8
use Nette\DI\Container;
9
use Portiny\RabbitMQ\Exchange\AbstractExchange;
10
use Portiny\RabbitMQ\Queue\AbstractQueue;
11
use React\EventLoop\LoopInterface;
12
use React\Promise\PromiseInterface;
13
14
final class BunnyManager
15
{
16
	/**
17
	 * @var array
18
	 */
19
	private $config = [];
20
21
	/**
22
	 * @var bool
23
	 */
24
	private $isDeclared = FALSE;
25
26
	/**
27
	 * @var Container
28
	 */
29
	private $container;
30
31
	/**
32
	 * @var LoopInterface
33
	 */
34
	private $loop;
35
36
	/**
37
	 * @var Client|AsyncClient|null
38
	 */
39
	private $client;
40
41
	/**
42
	 * @var Channel|PromiseInterface|null
43
	 */
44
	private $channel;
45
46 4
	public function __construct(Container $container, array $config)
47
	{
48 4
		$this->container = $container;
49 4
		$this->config = $config;
50 4
	}
51
52 1
	public function setLoop(LoopInterface $loop): void
53
	{
54 1
		$this->loop = $loop;
55 1
	}
56
57
	/**
58
	 * @return Client|AsyncClient
59
	 */
60 2
	public function getClient()
61
	{
62 2
		if ($this->client === null) {
63 2
			if ($this->loop === NULL) {
64 1
				$this->client = new Client($this->config['connection']);
65
			} else {
66 1
				$this->client = new AsyncClient($this->loop, $this->config['connection']);
67
			}
68
		}
69
70 2
		return $this->client;
71
	}
72
73
	/**
74
	 * @return Channel|PromiseInterface
75
	 */
76
	public function getChannel()
77
	{
78
		if ($this->channel === null) {
79
			$this->channel = $this->createChannel();
80
		}
81
82
		return $this->channel;
83
	}
84
85 1
	public function getClassNameByAlias(string $alias): ?string
86
	{
87 1
		return $this->config['aliases'][$alias] ?? null;
88
	}
89
90
	/**
91
	 * @return bool|PromiseInterface
92
	 */
93
	public function declare()
94
	{
95
		if ($this->isDeclared) {
96
			return false;
97
		}
98
99
		$channel = $this->getChannel();
100
101
		if ($channel instanceof PromiseInterface) {
0 ignored issues
show
Bug introduced by
The class React\Promise\PromiseInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
102
			$this->isDeclared = true;
103
104
			return $channel->then(function (Channel $channel) {
105
				$this->declareExchanges($channel);
106
				$this->declareQueues($channel);
107
108
				return true;
109
			});
110
		}
111
112
		$this->declareExchanges($channel);
113
		$this->declareQueues($channel);
114
115
		$this->isDeclared = true;
116
117
		return true;
118
	}
119
120
	/**
121
	 * @return Channel|PromiseInterface
122
	 */
123
	private function createChannel()
124
	{
125
		$client = $this->getClient();
126
127
		if (! $client->isConnected()) {
128
			if ($client instanceof AsyncClient) {
0 ignored issues
show
Bug introduced by
The class Bunny\Async\Client does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
129
				return $client->connect()->then(function (AsyncClient $client) {
130
					return $client->channel();
131
				});
132
			}
133
134
			$client->connect();
135
		}
136
137
		return $client->channel();
138
	}
139
140
	private function declareExchanges(Channel $channel): void
141
	{
142
		foreach ($this->config['exchanges'] as $exchangeClassName) {
143
			/** @var AbstractExchange $exchange */
144
			$exchange = $this->container->getByType($exchangeClassName);
145
			$exchange->declare($channel);
146
		}
147
148
		foreach ($this->config['exchanges'] as $exchangeClassName) {
149
			/** @var AbstractExchange $exchange */
150
			$exchange = $this->container->getByType($exchangeClassName);
151
			$exchange->declareBindings($channel);
152
		}
153
	}
154
155
	private function declareQueues(Channel $channel): void
156
	{
157
		foreach ($this->config['queues'] as $queueClassName) {
158
			/** @var AbstractQueue $queue */
159
			$queue = $this->container->getByType($queueClassName);
160
			$queue->declare($channel);
161
		}
162
	}
163
}
164