Failed Conditions
Pull Request — master (#60)
by Chad
01:49 queued 11s
created

Queue::verifySort()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 3
1
<?php
2
/**
3
 * Defines the TraderInteractive\Mongo\Queue class.
4
 */
5
6
namespace TraderInteractive\Mongo;
7
8
use MongoDB\BSON\UTCDateTime;
9
10
/**
11
 * Abstraction of mongo db collection as priority queue.
12
 *
13
 * Tied priorities are ordered by time. So you may use a single priority for normal queuing (default args exist for
14
 * this purpose).  Using a random priority achieves random get()
15
 */
16
final class Queue implements QueueInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: ack, ackSend, count, ensureCountIndex, ensureGetIndex, get, requeue, send
Loading history...
17
{
18
    /**
19
     * Maximum millisecond value to use for UTCDateTime creation.
20
     *
21
     * @var integer
22
     */
23
    const MONGO_INT32_MAX = PHP_INT_MAX;
24
25
    /**
26
     * mongo collection to use for queue.
27
     *
28
     * @var \MongoDB\Collection
29
     */
30
    private $collection;
31
32
    /**
33
     * Construct queue.
34
     *
35
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
36
     * @param string $db the mongo db name
37
     * @param string $collection the collection name to use for the queue
38
     *
39
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
40
     */
41
    public function __construct($collectionOrUrl, string $db = null, string $collection = null)
42
    {
43
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
44
            $this->collection = $collectionOrUrl;
45
            return;
46
        }
47
48
        if (!is_string($collectionOrUrl)) {
49
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
50
        }
51
52
        $mongo = new \MongoDB\Client(
53
            $collectionOrUrl,
54
            [],
55
            ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
56
        );
57
        $mongoDb = $mongo->selectDatabase($db);
58
        $this->collection = $mongoDb->selectCollection($collection);
59
    }
60
}
61