Issues (8)

src/Queue.php (3 issues)

Labels
1
<?php
2
/**
3
 * Defines the TraderInteractive\Mongo\Queue class.
4
 */
5
6
namespace TraderInteractive\Mongo;
7
8
use MongoDB\BSON\UTCDateTime;
9
use MongoDB\Client;
10
11
/**
12
 * Abstraction of mongo db collection as priority queue.
13
 *
14
 * Tied priorities are ordered by time. So you may use a single priority for normal queuing (default args exist for
15
 * this purpose).  Using a random priority achieves random get()
16
 */
17
final class Queue extends AbstractQueue implements QueueInterface
18
{
19
    /**
20
     * Construct queue.
21
     *
22
     * @param \MongoDB\Collection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
23
     * @param string $db the mongo db name
24
     * @param string $collection the collection name to use for the queue
25
     *
26
     * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
27
     */
28
    public function __construct($collectionOrUrl, string $db = null, string $collection = null)
29
    {
30
        if ($collectionOrUrl instanceof \MongoDB\Collection) {
31
            $this->collection = $collectionOrUrl;
32
            return;
33
        }
34
35
        if (!is_string($collectionOrUrl)) {
0 ignored issues
show
The condition is_string($collectionOrUrl) is always true.
Loading history...
36
            throw new \InvalidArgumentException('$collectionOrUrl was not a string');
37
        }
38
39
        $this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection($collection);
0 ignored issues
show
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

39
        $this->collection = (new Client($collectionOrUrl))->selectDatabase(/** @scrutinizer ignore-type */ $db)->selectCollection($collection);
Loading history...
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

39
        $this->collection = (new Client($collectionOrUrl))->selectDatabase($db)->selectCollection(/** @scrutinizer ignore-type */ $collection);
Loading history...
40
    }
41
}
42