TickFiniteQueue::__destruct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 5
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dazzle\Loop\Tick;
4
5
use Dazzle\Loop\LoopModelInterface;
6
use SplQueue;
7
8 View Code Duplication
class TickFiniteQueue
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    /**
11
     * @var LoopModelInterface
12
     */
13
    protected $loop;
14
15
    /**
16
     * @var SplQueue
17
     */
18
    protected $queue;
19
20
    /**
21
     * @var callable
22
     */
23
    private $callback;
24
25
    /**
26
     * @param LoopModelInterface $loop
27
     */
28 61
    public function __construct(LoopModelInterface $loop)
29
    {
30 61
        $this->loop = $loop;
31 61
        $this->queue = new SplQueue();
32 61
    }
33
34
    /**
35
     *
36
     */
37 10
    public function __destruct()
38
    {
39 10
        unset($this->loop);
40 10
        unset($this->queue);
41 10
    }
42
43
    /**
44
     * Add a callback to be invoked on a future tick of the event loop.
45
     *
46
     * Callbacks are guaranteed to be executed in the order they are enqueued, before any timer or stream events.
47
     *
48
     * @param callable $listener
49
     */
50 17
    public function add(callable $listener)
51
    {
52 17
        $this->queue->enqueue($listener);
53 17
    }
54
55
    /**
56
     * Flush the callback queue.
57
     *
58
     * Invokes as many callbacks as were on the queue when tick() was called.
59
     */
60 39
    public function tick()
61
    {
62 39
        $count = $this->queue->count();
63
64 39
        while ($count-- && $this->loop->isRunning())
65
        {
66 11
            $this->callback = $this->queue->dequeue();
67 11
            $callback = $this->callback; // without this proxy PHPStorm marks line as fatal error.
68 11
            $callback($this->loop);
69
        }
70 39
    }
71
72
    /**
73
     * Check if the next tick queue is empty.
74
     *
75
     * @return boolean
76
     */
77 6
    public function isEmpty()
78
    {
79 6
        return $this->queue->isEmpty();
80
    }
81
}
82