-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathUnorderedQueue.php
48 lines (42 loc) · 1.48 KB
/
UnorderedQueue.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
<?php
/**
* Defines the TraderInteractive\Mongo\UnorderedQueue class.
*/
namespace TraderInteractive\Mongo;
use MongoDB\Client;
use MongoDB\Operation\FindOneAndUpdate;
/**
* Abstraction of mongo db collection as unordered queue.
*/
final class UnorderedQueue extends AbstractQueue implements QueueInterface
{
/**
* Override from AbstractQueue class to remove sort options.
*
* @var array
*/
const FIND_ONE_AND_UPDATE_OPTIONS = [
'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
];
/**
* Construct queue.
*
* @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
* @param string $db the mongo db name
* @param string $collection the collection name to use for the queue
*
* @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
*/
public function __construct($collectionOrUrl, string $db = null, string $collection = null)
{
if ($collectionOrUrl instanceof \MongoDB\Collection) {
$this->collection = $collectionOrUrl;
return;
}
if (!is_string($collectionOrUrl)) {
throw new \InvalidArgumentException('$collectionOrUrl was not a string');
}
$this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection($collection);
}
}