1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ButterAMQP\AMQP091; |
4
|
|
|
|
5
|
|
|
use ButterAMQP\ExchangeInterface; |
6
|
|
|
use ButterAMQP\AMQP091\Framing\Frame; |
7
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeBind; |
8
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeBindOk; |
9
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeclare; |
10
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeclareOk; |
11
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeDelete; |
12
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeDeleteOk; |
13
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeUnbind; |
14
|
|
|
use ButterAMQP\AMQP091\Framing\Method\ExchangeUnbindOk; |
15
|
|
|
|
16
|
|
|
class Exchange implements ExchangeInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var WireInterface |
20
|
|
|
*/ |
21
|
|
|
private $wire; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $channel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $name; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param WireInterface $wire |
35
|
|
|
* @param int $channel |
36
|
|
|
* @param string $name |
37
|
|
|
*/ |
38
|
14 |
|
public function __construct(WireInterface $wire, $channel, $name) |
39
|
|
|
{ |
40
|
14 |
|
$this->wire = $wire; |
41
|
14 |
|
$this->channel = $channel; |
42
|
14 |
|
$this->name = $name; |
43
|
14 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
5 |
|
public function define($type, $flags = 0, array $arguments = []) |
49
|
|
|
{ |
50
|
5 |
|
$this->send(new ExchangeDeclare( |
51
|
5 |
|
$this->channel, |
52
|
5 |
|
0, |
53
|
5 |
|
$this->name, |
54
|
5 |
|
$type, |
55
|
5 |
|
(bool) ($flags & self::FLAG_PASSIVE), |
56
|
5 |
|
(bool) ($flags & self::FLAG_DURABLE), |
57
|
5 |
|
(bool) ($flags & self::FLAG_AUTO_DELETE), |
58
|
5 |
|
(bool) ($flags & self::FLAG_INTERNAL), |
59
|
5 |
|
(bool) ($flags & self::FLAG_NO_WAIT), |
60
|
|
|
$arguments |
61
|
5 |
|
)); |
62
|
|
|
|
63
|
5 |
|
if (!($flags & self::FLAG_NO_WAIT)) { |
64
|
4 |
|
$this->wait(ExchangeDeclareOk::class); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
5 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
4 |
|
public function delete($flags = 0) |
74
|
|
|
{ |
75
|
4 |
|
$this->send(new ExchangeDelete( |
76
|
4 |
|
$this->channel, |
77
|
4 |
|
0, |
78
|
4 |
|
$this->name, |
79
|
4 |
|
(bool) ($flags & self::FLAG_IF_UNUSED), |
80
|
4 |
|
(bool) ($flags & self::FLAG_NO_WAIT) |
81
|
4 |
|
)); |
82
|
|
|
|
83
|
4 |
|
if (!($flags & self::FLAG_NO_WAIT)) { |
84
|
3 |
|
$this->wait(ExchangeDeleteOk::class); |
85
|
3 |
|
} |
86
|
|
|
|
87
|
4 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
3 |
View Code Duplication |
public function bind($exchange, $routingKey = '', array $arguments = [], $flags = 0) |
|
|
|
|
94
|
|
|
{ |
95
|
3 |
|
$this->send(new ExchangeBind( |
96
|
3 |
|
$this->channel, |
97
|
3 |
|
0, |
98
|
3 |
|
$exchange, |
99
|
3 |
|
$this->name, |
100
|
3 |
|
$routingKey, |
101
|
3 |
|
(bool) ($flags & self::FLAG_NO_WAIT), |
102
|
|
|
$arguments |
103
|
3 |
|
)); |
104
|
|
|
|
105
|
3 |
|
if (!($flags & self::FLAG_NO_WAIT)) { |
106
|
1 |
|
$this->wait(ExchangeBindOk::class); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
3 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
1 |
View Code Duplication |
public function unbind($exchange, $routingKey = '', array $arguments = [], $flags = 0) |
|
|
|
|
116
|
|
|
{ |
117
|
1 |
|
$this->send(new ExchangeUnbind( |
118
|
1 |
|
$this->channel, |
119
|
1 |
|
0, |
120
|
1 |
|
$exchange, |
121
|
1 |
|
$this->name, |
122
|
1 |
|
$routingKey, |
123
|
1 |
|
(bool) ($flags & self::FLAG_NO_WAIT), |
124
|
|
|
$arguments |
125
|
1 |
|
)); |
126
|
|
|
|
127
|
1 |
|
if (!($flags & self::FLAG_NO_WAIT)) { |
128
|
1 |
|
$this->wait(ExchangeUnbindOk::class); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
2 |
|
public function name() |
138
|
|
|
{ |
139
|
2 |
|
return $this->name; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
3 |
|
public function __toString() |
146
|
|
|
{ |
147
|
3 |
|
return $this->name; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Frame $frame |
152
|
|
|
* |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
11 |
|
private function send(Frame $frame) |
156
|
|
|
{ |
157
|
11 |
|
$this->wire->send($frame); |
158
|
|
|
|
159
|
11 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string|array $type |
164
|
|
|
* |
165
|
|
|
* @return Frame |
166
|
|
|
*/ |
167
|
7 |
|
private function wait($type) |
168
|
|
|
{ |
169
|
7 |
|
return $this->wire->wait($this->channel, $type); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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.