UnorderedQueue   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
1
<?php
2
/**
3
 * Defines the TraderInteractive\Mongo\UnorderedQueue class.
4
 */
5
6
namespace TraderInteractive\Mongo;
7
8
use MongoDB\Client;
9
use MongoDB\Operation\FindOneAndUpdate;
10
11
/**
12
 * Abstraction of mongo db collection as unordered queue.
13
 */
14
final class UnorderedQueue extends AbstractQueue implements QueueInterface
15
{
16
    /**
17
     * Override from AbstractQueue class to remove sort options.
18
     *
19
     * @var array
20
     */
21
    const FIND_ONE_AND_UPDATE_OPTIONS = [
22
        'typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array'],
23
        'returnDocument' => FindOneAndUpdate::RETURN_DOCUMENT_AFTER,
24
    ];
25
26
    /**
27
     * Construct queue.
28
     *
29
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
30
     * @param string $db the mongo db name
31
     * @param string $collection the collection name to use for the queue
32
     *
33
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
34
     */
35
    public function __construct($collectionOrUrl, string $db = null, string $collection = null)
36
    {
37
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
38
            $this->collection = $collectionOrUrl;
39
            return;
40
        }
41
42
        if (!is_string($collectionOrUrl)) {
0 ignored issues
show
introduced by
The condition is_string($collectionOrUrl) is always true.
Loading history...
43
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
44
        }
45
46
        $this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection($collection);
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $databaseName of MongoDB\Client::selectDatabase() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $this->collection = (new Client($collectionOrUrl))->selectDatabase(/** @scrutinizer ignore-type */ $db)->selectCollection($collection);
Loading history...
Bug introduced by
It seems like $collection can also be of type null; however, parameter $collectionName of MongoDB\Database::selectCollection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
        $this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection(/** @scrutinizer ignore-type */ $collection);
Loading history...
47
    }
48
}
49