Passed
Pull Request — master (#60)
by Chad
05:10
created

Queue::ackSend()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 14
nop 5
1
<?php
2
/**
3
 * Defines the TraderInteractive\Mongo\Queue class.
4
 */
5
6
namespace TraderInteractive\Mongo;
7
8
/**
9
 * Implementation of mongo db collection as priority queue.
10
 *
11
 * Tied priorities are ordered by time. So you may use a single priority for normal queuing (default args exist for
12
 * this purpose).  Using a random priority achieves random get()
13
 */
14
final class Queue extends AbstractQueue implements QueueInterface
15
{
16
    /**
17
     * Construct queue.
18
     *
19
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
20
     * @param string $db the mongo db name
21
     * @param string $collection the collection name to use for the queue
22
     *
23
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
24
     */
25
    public function __construct($collectionOrUrl, string $db = null, string $collection = null)
26
    {
27
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
28
            $this->collection = $collectionOrUrl;
29
            return;
30
        }
31
32
        if (!is_string($collectionOrUrl)) {
33
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
34
        }
35
36
        $mongo = new \MongoDB\Client(
37
            $collectionOrUrl,
38
            [],
39
            ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
40
        );
41
        $mongoDb = $mongo->selectDatabase($db);
42
        $this->collection = $mongoDb->selectCollection($collection);
43
    }
44
}
45