MongoQueue::pop()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Phive Queue package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phive\Queue;
13
14
class MongoQueue implements Queue
15
{
16
    /**
17
     * @var \MongoClient
18
     */
19
    private $mongoClient;
20
21
    /**
22
     * @var string
23
     */
24
    private $dbName;
25
26
    /**
27
     * @var string
28
     */
29
    private $collName;
30
31
    /**
32
     * @var \MongoCollection
33
     */
34
    private $coll;
35
36 15
    public function __construct(\MongoClient $mongoClient, $dbName, $collName)
37
    {
38 15
        $this->mongoClient = $mongoClient;
39 15
        $this->dbName = $dbName;
40 15
        $this->collName = $collName;
41 15
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 14
    public function push($item, $eta = null)
47
    {
48
        $doc = [
49 14
            'eta'  => QueueUtils::normalizeEta($eta),
50 14
            'item' => $item,
51 14
        ];
52
53 14
        $this->getCollection()->insert($doc);
54 13
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 12
    public function pop()
60
    {
61 12
        $result = $this->getCollection()->findAndModify(
62 12
             ['eta' => ['$lte' => time()]],
63 12
             [],
64 12
             ['item' => 1, '_id' => 0],
65 12
             ['remove' => 1, 'sort' => ['eta' => 1]]
66 12
        );
67
68 12
        if ($result && array_key_exists('item', $result)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69 12
            return $result['item'];
70
        }
71
72 2
        throw new NoItemAvailableException($this);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function count()
79
    {
80 1
        return $this->getCollection()->count();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 1
    public function clear()
87
    {
88 1
        $this->getCollection()->remove();
89 1
    }
90
91 14
    protected function getCollection()
92
    {
93 14
        if (!$this->coll) {
94 14
            $this->coll = $this->mongoClient->selectCollection(
95 14
                $this->dbName,
96 14
                $this->collName
97 14
            );
98 14
        }
99
100 14
        return $this->coll;
101
    }
102
}
103