Failed Conditions
Pull Request — master (#52)
by Chad
02:54 queued 23s
created

Queue::ensureIndex()   C

Complexity

Conditions 8
Paths 17

Size

Total Lines 33
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 14
nc 17
nop 1
1
<?php
2
/**
3
 * Defines the DominionEnterprises\Mongo\Queue class.
4
 */
5
6
namespace DominionEnterprises\Mongo;
7
8
/**
9
 * Abstraction 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
    /**
18
     * Construct queue.
19
     *
20
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
21
     * @param string $db the mongo db name
22
     * @param string $collection the collection name to use for the queue
23
     *
24
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
25
     */
26
    public function __construct($collectionOrUrl, $db = null, $collection = null)
27
    {
28
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
29
            $this->collection = $collectionOrUrl;
30
            return;
31
        }
32
33
        if (!is_string($collectionOrUrl)) {
34
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
35
        }
36
37
        if (!is_string($db)) {
38
            throw new \InvalidArgumentException('$db was not a string');
39
        }
40
41
        if (!is_string($collection)) {
42
            throw new \InvalidArgumentException('$collection was not a string');
43
        }
44
45
        $mongo = new \MongoDB\Client($collectionOrUrl, [], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
46
        $mongoDb = $mongo->selectDatabase($db);
47
        $this->collection = $mongoDb->selectCollection($collection);
48
    }
49
}
50