Test Failed
Push — master ( b17eba...69b436 )
by Kirill
06:37
created

Queue::reduce()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Processor;
11
12
/**
13
 * Class DeferredStack
14
 */
15
class Queue implements \IteratorAggregate
16
{
17
    /**
18
     * @var \SplQueue
19
     */
20
    private $queue;
21
22
    /**
23
     * Queue constructor.
24
     */
25
    public function __construct()
26
    {
27
        $this->queue = new \SplQueue();
28
    }
29
30
    /**
31
     * @param \Closure $deferred
32
     * @return Queue
33
     */
34
    public function push(\Closure $deferred): Queue
35
    {
36
        $this->queue->push($deferred);
37
38
        return $this;
39
    }
40
41
    /**
42
     * @return \Generator|\Closure[]
43
     */
44
    public function getIterator(): \Generator
45
    {
46
        while ($this->queue->count()) {
47
            yield $this->queue->pop();
48
        }
49
    }
50
51
    /**
52
     * @param array $args
53
     * @return \Generator
54
     */
55
    public function reduce(...$args): \Generator
56
    {
57
        foreach ($this->getIterator() as $item) {
58
            $output = $item(...$args);
59
60
            if ($output instanceof \Traversable) {
61
                yield $output;
62
            }
63
        }
64
    }
65
}
66