Failed Conditions
Pull Request — master (#52)
by Chad
03:14
created

Queue::get()   C

Complexity

Conditions 14
Paths 101

Size

Total Lines 78
Code Lines 36

Duplication

Lines 7
Ratio 8.97 %

Importance

Changes 0
Metric Value
dl 7
loc 78
rs 5.138
c 0
b 0
f 0
cc 14
eloc 36
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 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