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

AbstractExchange::declareBindings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php declare(strict_types=1);
2
3
namespace Portiny\RabbitMQ\Exchange;
4
5
use Bunny\Channel;
6
use Bunny\Exception\BunnyException;
7
use Bunny\Protocol\MethodExchangeBindOkFrame;
8
use Bunny\Protocol\MethodExchangeDeclareOkFrame;
9
10
abstract class AbstractExchange
11
{
12
	/**
13
	 * @var string
14
	 */
15
	public const TYPE_DIRECT = 'direct';
16
17
	/**
18
	 * @var string
19
	 */
20
	public const TYPE_HEADERS = 'headers';
21
22
	/**
23
	 * @var string
24
	 */
25
	public const TYPE_FANOUT = 'fanout';
26
27
	/**
28
	 * @var string
29
	 */
30
	public const TYPE_TOPIC = 'topic';
31
32
	/**
33
	 * @var array
34
	 */
35
	public const AVAILABLE_TYPES = [self::TYPE_DIRECT, self::TYPE_HEADERS, self::TYPE_FANOUT, self::TYPE_TOPIC];
36
37
	/**
38
	 * @throws BunnyException
39
	 */
40
	final public function declare(Channel $channel): void
41
	{
42
		$frame = $channel->exchangeDeclare(
43
			$this->getName(),
44
			$this->getType(),
45
			$this->isPassive(),
46
			$this->isDurable(),
47
			$this->isAutoDelete(),
48
			$this->isInternal(),
49
			FALSE,
50
			$this->getArguments()
51
		);
52
		if (! $frame instanceof MethodExchangeDeclareOkFrame) {
0 ignored issues
show
Bug introduced by
The class Bunny\Protocol\MethodExchangeDeclareOkFrame 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...
53
			throw new BunnyException(sprintf('Could not declare exchange "%s".', $this->getName()));
54
		}
55
	}
56
57
	/**
58
	 * @throws BunnyException
59
	 */
60
	final public function declareBindings(Channel $channel): void
61
	{
62
		foreach ($this->getBindings() as $exchangeBind) {
63
			$frame = $channel->exchangeBind(
64
				$exchangeBind->getDestination(),
65
				$this->getName(),
66
				$exchangeBind->getRoutingKey(),
67
				FALSE,
68
				$exchangeBind->getArguments()
69
			);
70
			if (! $frame instanceof MethodExchangeBindOkFrame) {
0 ignored issues
show
Bug introduced by
The class Bunny\Protocol\MethodExchangeBindOkFrame 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...
71
				throw new BunnyException(
72
					sprintf(
73
						'Could not bind exchange "%s" to "%s" with routing key "%s".',
74
						$exchangeBind->getDestination(),
75
						$this->getName(),
76
						$exchangeBind->getRoutingKey()
77
					)
78
				);
79
			}
80
		}
81
	}
82
83
	abstract protected function getName(): string;
84
85
	/**
86
	 * @return ExchangeBind[]
87
	 */
88
	protected function getBindings(): array
89
	{
90
		return [];
91
	}
92
93
	protected function getType(): string
94
	{
95
		return self::TYPE_DIRECT;
96
	}
97
98
	protected function isPassive(): bool
99
	{
100
		return false;
101
	}
102
103
	protected function isDurable(): bool
104
	{
105
		return false;
106
	}
107
108
	protected function isAutoDelete(): bool
109
	{
110
		return false;
111
	}
112
113
	protected function isInternal(): bool
114
	{
115
		return false;
116
	}
117
118
	protected function isNoWait(): bool
119
	{
120
		return false;
121
	}
122
123
	protected function getArguments(): array
124
	{
125
		return [];
126
	}
127
}
128