|
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
|
|
|
public const TYPE_DIRECT = 'direct'; |
|
13
|
|
|
|
|
14
|
|
|
public const TYPE_HEADERS = 'headers'; |
|
15
|
|
|
|
|
16
|
|
|
public const TYPE_FANOUT = 'fanout'; |
|
17
|
|
|
|
|
18
|
|
|
public const TYPE_TOPIC = 'topic'; |
|
19
|
|
|
|
|
20
|
|
|
public const AVAILABLE_TYPES = [self::TYPE_DIRECT, self::TYPE_HEADERS, self::TYPE_FANOUT, self::TYPE_TOPIC]; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @throws BunnyException |
|
25
|
|
|
*/ |
|
26
|
|
|
final public function declare(Channel $channel): void |
|
27
|
|
|
{ |
|
28
|
|
|
$frame = $channel->exchangeDeclare( |
|
29
|
|
|
$this->getName(), |
|
30
|
|
|
$this->getType(), |
|
31
|
|
|
$this->isPassive(), |
|
32
|
|
|
$this->isDurable(), |
|
33
|
|
|
$this->isAutoDelete(), |
|
34
|
|
|
$this->isInternal(), |
|
35
|
|
|
false, |
|
36
|
|
|
$this->getArguments() |
|
37
|
|
|
); |
|
38
|
|
|
if (! $frame instanceof MethodExchangeDeclareOkFrame) { |
|
39
|
|
|
throw new BunnyException(sprintf('Could not declare exchange "%s".', $this->getName())); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws BunnyException |
|
46
|
|
|
*/ |
|
47
|
|
|
final public function declareBindings(Channel $channel): void |
|
48
|
|
|
{ |
|
49
|
|
|
foreach ($this->getBindings() as $exchangeBind) { |
|
50
|
|
|
$frame = $channel->exchangeBind( |
|
51
|
|
|
$exchangeBind->getDestination(), |
|
52
|
|
|
$this->getName(), |
|
53
|
|
|
$exchangeBind->getRoutingKey(), |
|
54
|
|
|
false, |
|
55
|
|
|
$exchangeBind->getArguments() |
|
56
|
|
|
); |
|
57
|
|
|
if (! $frame instanceof MethodExchangeBindOkFrame) { |
|
58
|
|
|
throw new BunnyException( |
|
59
|
|
|
sprintf( |
|
60
|
|
|
'Could not bind exchange "%s" to "%s" with routing key "%s".', |
|
61
|
|
|
$exchangeBind->getDestination(), |
|
62
|
|
|
$this->getName(), |
|
63
|
|
|
$exchangeBind->getRoutingKey() |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
abstract protected function getName(): string; |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return ExchangeBind[] |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getBindings(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return []; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
protected function getType(): string |
|
84
|
|
|
{ |
|
85
|
|
|
return self::TYPE_DIRECT; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
protected function isPassive(): bool |
|
90
|
|
|
{ |
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
protected function isDurable(): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
protected function isAutoDelete(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
protected function isInternal(): bool |
|
108
|
|
|
{ |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
protected function isNoWait(): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
protected function getArguments(): array |
|
120
|
|
|
{ |
|
121
|
|
|
return []; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|