-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArrayQueueDriver.php
More file actions
174 lines (149 loc) · 4.39 KB
/
ArrayQueueDriver.php
File metadata and controls
174 lines (149 loc) · 4.39 KB
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
167
168
169
170
171
172
173
174
<?php
namespace Smartbox\Integration\FrameworkBundle\Components\Queues\Drivers;
use Smartbox\Integration\FrameworkBundle\Components\Queues\QueueMessage;
use Smartbox\Integration\FrameworkBundle\Components\Queues\QueueMessageInterface;
use Smartbox\Integration\FrameworkBundle\Core\Messages\Context;
use Smartbox\Integration\FrameworkBundle\Service;
/**
* Class ArrayQueueDriver.
*/
class ArrayQueueDriver extends Service implements QueueDriverInterface
{
public static $array = [];
protected $connected = false;
protected $subscribedQueue = false;
protected $unacknowledgedFrame = null;
/**
* @return int
*/
public function getDequeueingTimeMs()
{
return 0;
}
/**
* @return array
*/
public function getArrayForQueue($queue)
{
if (!array_key_exists($queue, self::$array)) {
self::$array[$queue] = [];
}
return self::$array[$queue];
}
/**
* Configures the driver.
*
* @param string $uri URI of the queuing system
* @param string $username Username to connect to the queuing system
* @param string $password Password to connect to the queuing system
*/
public function configure($host, $username, $password, $format = '')
{
self::$array = [];
}
/**
* Opens a connection with a queuing system.
*/
public function connect()
{
$this->connected = true;
}
/**
* Destroys the connection with the queuing system.
*/
public function disconnect()
{
$this->connected = false;
}
/**
* Returns true if a connection already exists with the queing system, false otherwise.
*
* @return bool
*/
public function isConnected()
{
return $this->connected;
}
/**
* Returns true if a subscription already exists, false otherwise.
*
* @return bool
*/
public function isSubscribed()
{
return !empty($this->subscribedQueue);
}
/**
* Creates a subscription to the given $queue, allowing to receive messages from it.
*
* @param string $queue Queue to subscribe
* @param string|null $selector If supported, it is an expression filters the messages on the queue
*/
public function subscribe($queue, $selector = null)
{
$this->subscribedQueue = $queue;
}
/**
* Destroys the created subscription with a queue.
*/
public function unSubscribe()
{
$this->subscribedQueue = false;
}
/**
* Acknowledges the processing of the last received object.
*
* The object should be removed from the queue.
*/
public function ack()
{
$this->unacknowledgedFrame = false;
}
/**
* Acknowledges a failure on processing the last received object.
*
* The object could be moved to the DLQ or be delivered to another subscription for retrial
*/
public function nack()
{
$this->unacknowledgedFrame = false;
}
/** {@inheritdoc} */
public function send(QueueMessageInterface $message, $destination = null)
{
$destination = $destination ? $destination : $message->getQueue();
if (!array_key_exists($destination, self::$array)) {
self::$array[$destination] = [];
}
self::$array[$destination][] = $message;
return true;
}
/** {@inheritdoc} */
public function receive()
{
if (array_key_exists($this->subscribedQueue, self::$array) && !empty(self::$array[$this->subscribedQueue])) {
$this->unacknowledgedFrame = array_shift(self::$array[$this->subscribedQueue]);
}
return $this->unacknowledgedFrame;
}
/**
* @return \Smartbox\Integration\FrameworkBundle\Components\Queues\QueueMessageInterface
*/
public function createQueueMessage()
{
/*
* This driver will ignore all the headers so it can use any message that implements QueueMessageInterface
*/
$msg = new QueueMessage();
$msg->setContext(new Context([Context::FLOWS_VERSION => $this->getFlowsVersion()]));
return $msg;
}
/**
* Clean all the opened resources, must be called just before terminating the current request.
*/
public function doDestroy()
{
// TODO: Implement doDestroy() method.
// I have no time to do destroy the world.
}
}