Completed
Pull Request — master (#60)
by Chad
01:35
created

Queue::get()   C

Complexity

Conditions 13
Paths 101

Size

Total Lines 71
Code Lines 34

Duplication

Lines 7
Ratio 9.86 %

Importance

Changes 0
Metric Value
dl 7
loc 71
rs 5.5441
c 0
b 0
f 0
cc 13
eloc 34
nc 101
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 extends AbstractQueue implements QueueInterface
17
{
18
    /**
19
     * Construct queue.
20
     *
21
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
22
     * @param string $db the mongo db name
23
     * @param string $collection the collection name to use for the queue
24
     *
25
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
26
     */
27
    public function __construct($collectionOrUrl, string $db = null, string $collection = null)
28
    {
29
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
30
            $this->collection = $collectionOrUrl;
31
            return;
32
        }
33
34
        if (!is_string($collectionOrUrl)) {
35
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
36
        }
37
38
        $mongo = new \MongoDB\Client(
39
            $collectionOrUrl,
40
            [],
41
            ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
42
        );
43
        $mongoDb = $mongo->selectDatabase($db);
44
        $this->collection = $mongoDb->selectCollection($collection);
45
    }
46
}
47