Test Failed
Push — master ( 1c5165...b7ef6a )
by Kirill
03:37
created

Queue   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 39.13%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 67
ccs 9
cts 23
cp 0.3913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A push() 0 6 1
A getIterator() 0 6 2
A reduce() 0 10 3
A extract() 0 11 4
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 30
    public function __construct()
26
    {
27 30
        $this->queue = new \SplQueue();
28 30
    }
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 30
    public function getIterator(): \Generator
45
    {
46 30
        while ($this->queue->count()) {
47
            yield $this->queue->pop();
48
        }
49 30
    }
50
51
    /**
52
     * @param iterable $result
53
     * @return \Generator
54
     */
55 30
    public function reduce(iterable $result): \Generator
56
    {
57 30
        foreach ($this->getIterator() as $item) {
58
            $output = $item($this->extract($result));
59
60
            if ($output instanceof \Traversable) {
61
                yield $output;
62
            }
63
        }
64 30
    }
65
66
    /**
67
     * @param iterable $result
68
     * @return \Generator
69
     */
70
    private function extract(iterable $result): \Generator
71
    {
72
        foreach ($result as $item) {
73
            if (isset($item[0]) && ! \is_scalar($item[0])) {
74
                $entity = \array_shift($item);
75
                yield $entity => $item;
76
            } else {
77
                yield $item => [];
78
            }
79
        }
80
    }
81
}
82