-
Notifications
You must be signed in to change notification settings - Fork 7
/
BunnyManager.php
166 lines (137 loc) · 3.82 KB
/
BunnyManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace Skrz\Bundle\BunnyBundle;
use Bunny\Channel;
use Bunny\Client;
use Bunny\Protocol\MethodExchangeBindOkFrame;
use Bunny\Protocol\MethodExchangeDeclareOkFrame;
use Bunny\Protocol\MethodQueueBindOkFrame;
use Bunny\Protocol\MethodQueueDeclareOkFrame;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BunnyManager
{
/** @var ContainerInterface */
private $container;
/** @var Client */
private $client;
/** @var Channel */
private $channel;
/** @var Channel */
private $transactionalChannel;
/** @var array */
private $config;
/** @var boolean */
private $setUpComplete = false;
public function __construct(ContainerInterface $container, $clientServiceId, array $config)
{
$this->container = $container;
$this->clientServiceId = $clientServiceId;
$this->config = $config;
}
public function getClient()
{
if ($this->client === null) {
$this->client = $this->container->get($this->clientServiceId);
}
return $this->client;
}
public function createChannel()
{
if (!$this->getClient()->isConnected()) {
$this->getClient()->connect();
}
return $this->getClient()->channel();
}
public function getChannel()
{
if (!$this->channel) {
$this->channel = $this->createChannel();
}
return $this->channel;
}
/**
* create/return transactional channel, where messages need to be commited
*
* @throws BunnyException
* @return Channel|\React\Promise\PromiseInterface
*/
public function getTransactionalChannel()
{
if (!$this->transactionalChannel) {
$this->transactionalChannel = $this->createChannel();
// create transactional channel from normal one
try {
$this->transactionalChannel->txSelect();
} catch (\Exception $e) {
throw new BunnyException("Cannot create transaction channel.");
}
}
return $this->transactionalChannel;
}
public function setUp()
{
if ($this->setUpComplete) {
return;
}
$channel = $this->getChannel();
foreach ($this->config["exchanges"] as $exchangeName => $exchangeDefinition) {
$frame = $channel->exchangeDeclare(
$exchangeName,
$exchangeDefinition["type"],
false,
$exchangeDefinition["durable"],
$exchangeDefinition["auto_delete"],
$exchangeDefinition["internal"],
false,
$exchangeDefinition["arguments"]
);
if (!($frame instanceof MethodExchangeDeclareOkFrame)) {
throw new BunnyException("Could not declare exchange '{$exchangeName}'.");
}
}
foreach ($this->config["exchanges"] as $exchangeName => $exchangeDefinition) {
foreach ($exchangeDefinition["bindings"] as $binding) {
$frame = $channel->exchangeBind(
$exchangeName,
$binding["exchange"],
$binding["routing_key"],
false,
$binding["arguments"]
);
if (!($frame instanceof MethodExchangeBindOkFrame)) {
throw new BunnyException(
"Could not bind exchange '{$exchangeName}' to '{$binding["exchange"]}' with routing key '{$binding["routing_key"]}'."
);
}
}
}
foreach ($this->config["queues"] as $queueName => $queueDefinition) {
$frame = $channel->queueDeclare(
$queueName,
false,
$queueDefinition["durable"],
$queueDefinition["exclusive"],
$queueDefinition["auto_delete"],
false,
$queueDefinition["arguments"]
);
if (!($frame instanceof MethodQueueDeclareOkFrame)) {
throw new BunnyException("Could not declare queue '{$queueName}'.");
}
foreach ($queueDefinition["bindings"] as $binding) {
$frame = $channel->queueBind(
$queueName,
$binding["exchange"],
$binding["routing_key"],
false,
$binding["arguments"]
);
if (!($frame instanceof MethodQueueBindOkFrame)) {
throw new BunnyException(
"Could not bind queue '{$queueName}' to '{$binding["exchange"]}' with routing key '{$binding["routing_key"]}'."
);
}
}
}
$this->setUpComplete = true;
}
}