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

Queue   C

Complexity

Total Complexity 65

Size/Duplication

Total Lines 466
Duplicated Lines 3 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 65
lcom 1
cbo 3
dl 14
loc 466
rs 5.7894
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Queue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Queue, and based on these observations, apply Extract Interface, too.

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