InMemoryQueue::pop()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
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 InMemoryQueue implements Queue
15
{
16
    /**
17
     * @var \SplPriorityQueue
18
     */
19
    private $queue;
20
21
    /**
22
     * @var int
23
     */
24
    private $queueOrder;
25
26 15
    public function __construct()
27
    {
28 15
        $this->queue = new \SplPriorityQueue();
29 15
        $this->queueOrder = PHP_INT_MAX;
30 15
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 14
    public function push($item, $eta = null)
36
    {
37 14
        $eta = QueueUtils::normalizeEta($eta);
38 14
        $this->queue->insert($item, [-$eta, $this->queueOrder--]);
39 14
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 13
    public function pop()
45
    {
46 13
        if (!$this->queue->isEmpty()) {
47 13
            $this->queue->setExtractFlags(\SplPriorityQueue::EXTR_PRIORITY);
48 13
            $priority = $this->queue->top();
49
50 13
            if (time() + $priority[0] >= 0) {
51 13
                $this->queue->setExtractFlags(\SplPriorityQueue::EXTR_DATA);
52
53 13
                return $this->queue->extract();
54
            }
55 1
        }
56
57 2
        throw new NoItemAvailableException($this);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function count()
64
    {
65 1
        return $this->queue->count();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public function clear()
72
    {
73 1
        $this->queue = new \SplPriorityQueue();
74 1
        $this->queueOrder = PHP_INT_MAX;
75 1
    }
76
}
77