NotificationSenderQueue.php
1.38 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Settings;
class NotificationSenderQueue {
/**
* @var \SplObjectStorage<UpdateNotificationSender, bool>
*/
protected $isInQueue;
/**
* @var \SplQueue<UpdateNotificationSender>
*/
protected $queue;
public function __construct() {
$this->queue = new \SplQueue();
$this->isInQueue = new \SplObjectStorage();
}
public function enqueue(UpdateNotificationSender $setting) {
if ( $this->isInQueue->contains($setting) ) {
//Already in the queue. Let's just mark it as valid.
$this->isInQueue[$setting] = true;
} else {
//Add to the queue.
$this->isInQueue->attach($setting, true);
$this->queue->enqueue($setting);
}
}
public function dequeue() {
//Find and return the first valid (non-removed) item.
while (!$this->queue->isEmpty()) {
$sender = $this->queue->dequeue();
if ( $this->isInQueue[$sender] ) {
$this->isInQueue->detach($sender);
return $sender;
}
}
return null;
}
public function remove(UpdateNotificationSender $setting) {
if ( $this->isInQueue->contains($setting) ) {
//There's not a quick way to remove an element from a SplQueue,
//so we'll just mark the item as invalid. It will be removed
//in dequeue().
$this->isInQueue[$setting] = false;
}
}
public function isEmpty() {
return ($this->queue->isEmpty() || ($this->isInQueue->count() < 1));
}
}