AbstractQueue::declare()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 30
rs 9.52
ccs 0
cts 25
cp 0
cc 4
nc 4
nop 1
crap 20
1
<?php declare(strict_types = 1);
2
3
namespace Portiny\RabbitMQ\Queue;
4
5
use Bunny\Channel;
6
use Bunny\Exception\BunnyException;
7
use Bunny\Protocol\MethodQueueBindOkFrame;
8
use Bunny\Protocol\MethodQueueDeclareOkFrame;
9
10
abstract class AbstractQueue
11
{
12
13
	final public function declare(Channel $channel): void
14
	{
15
		$frame = $channel->queueDeclare(
16
			$this->getName(),
17
			$this->isPassive(),
18
			$this->isDurable(),
19
			$this->isExclusive(),
20
			$this->isAutoDelete(),
21
			false,
22
			$this->getArguments()
23
		);
24
		if (! $frame instanceof MethodQueueDeclareOkFrame) {
25
			throw new BunnyException(sprintf('Could not declare queue "%s".', $this->getName()));
26
		}
27
28
		foreach ($this->getBindings() as $queueBind) {
29
			$frame = $channel->queueBind(
30
				$this->getName(),
31
				$queueBind->getExchange(),
32
				$queueBind->getRoutingKey(),
33
				false,
34
				$queueBind->getArguments()
35
			);
36
			if (! $frame instanceof MethodQueueBindOkFrame) {
37
				throw new BunnyException(
38
					sprintf(
39
						'Could not bind queue "%s" to "%s" with routing key "%s".',
40
						$this->getName(),
41
						$queueBind->getExchange(),
42
						$queueBind->getRoutingKey()
43
					)
44
				);
45
			}
46
		}
47
	}
48
49
50
	abstract protected function getName(): string;
51
52
53
	protected function isPassive(): bool
54
	{
55
		return false;
56
	}
57
58
59
	protected function isDurable(): bool
60
	{
61
		return false;
62
	}
63
64
65
	protected function isExclusive(): bool
66
	{
67
		return false;
68
	}
69
70
71
	protected function isAutoDelete(): bool
72
	{
73
		return false;
74
	}
75
76
77
	protected function isNoWait(): bool
78
	{
79
		return false;
80
	}
81
82
83
	protected function getArguments(): array
84
	{
85
		return [];
86
	}
87
88
89
	/**
90
	 * @return QueueBind[]
91
	 */
92
	protected function getBindings(): array
93
	{
94
		return [];
95
	}
96
97
}
98