Code Duplication    Length = 72-74 lines in 2 locations

src/Loop/Tick/TickContinousQueue.php 1 location

@@ 8-79 (lines=72) @@
5
use Dazzle\Loop\LoopModelInterface;
6
use SplQueue;
7
8
class TickContinousQueue
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
    public function __construct(LoopModelInterface $loop)
29
    {
30
        $this->loop = $loop;
31
        $this->queue = new SplQueue();
32
    }
33
34
    /**
35
     *
36
     */
37
    public function __destruct()
38
    {
39
        unset($this->loop);
40
        unset($this->queue);
41
    }
42
43
    /**
44
     * Add a callback to be invoked on the next 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
    public function add(callable $listener)
51
    {
52
        $this->queue->enqueue($listener);
53
    }
54
55
    /**
56
     * Flush the callback queue.
57
     *
58
     * Invokes callbacks which were on the queue when tick() was called and newly added ones.
59
     */
60
    public function tick()
61
    {
62
        while (!$this->queue->isEmpty() && $this->loop->isRunning())
63
        {
64
            $this->callback = $this->queue->dequeue();
65
            $callback = $this->callback; // without this proxy PHPStorm marks line as fatal error.
66
            $callback($this->loop);
67
        }
68
    }
69
70
    /**
71
     * Check if the next tick queue is empty.
72
     *
73
     * @return boolean
74
     */
75
    public function isEmpty()
76
    {
77
        return $this->queue->isEmpty();
78
    }
79
}
80

src/Loop/Tick/TickFiniteQueue.php 1 location

@@ 8-81 (lines=74) @@
5
use Dazzle\Loop\LoopModelInterface;
6
use SplQueue;
7
8
class TickFiniteQueue
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
    public function __construct(LoopModelInterface $loop)
29
    {
30
        $this->loop = $loop;
31
        $this->queue = new SplQueue();
32
    }
33
34
    /**
35
     *
36
     */
37
    public function __destruct()
38
    {
39
        unset($this->loop);
40
        unset($this->queue);
41
    }
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
    public function add(callable $listener)
51
    {
52
        $this->queue->enqueue($listener);
53
    }
54
55
    /**
56
     * Flush the callback queue.
57
     *
58
     * Invokes as many callbacks as were on the queue when tick() was called.
59
     */
60
    public function tick()
61
    {
62
        $count = $this->queue->count();
63
64
        while ($count-- && $this->loop->isRunning())
65
        {
66
            $this->callback = $this->queue->dequeue();
67
            $callback = $this->callback; // without this proxy PHPStorm marks line as fatal error.
68
            $callback($this->loop);
69
        }
70
    }
71
72
    /**
73
     * Check if the next tick queue is empty.
74
     *
75
     * @return boolean
76
     */
77
    public function isEmpty()
78
    {
79
        return $this->queue->isEmpty();
80
    }
81
}
82