Passed
Push — master ( 7e60c8...82eb11 )
by Fabrice
02:38
created

AggregateNode::getNodeCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of NodalFlow.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\NodalFlow\Nodes;
11
12
use fab2s\NodalFlow\Flows\FlowInterface;
13
use fab2s\NodalFlow\NodalFlow;
14
use fab2s\NodalFlow\NodalFlowException;
15
16
/**
17
 * class AggregateNode
18
 */
19
class AggregateNode extends PayloadNodeAbstract implements AggregateNodeInterface
20
{
21
    /**
22
     * The underlying pseudo Flow
23
     *
24
     * @var FlowInterface
25
     */
26
    protected $payload;
27
28
    /**
29
     * Instantiate an Aggregate Node
30
     *
31
     * @param bool $isAReturningVal
32
     *
33
     * @throws NodalFlowException
34
     */
35
    public function __construct($isAReturningVal)
36
    {
37
        parent::__construct(new NodalFlow, $isAReturningVal);
38
        $this->isATraversable = true;
39
        $this->isAFlow        = false;
40
    }
41
42
    /**
43
     * Add a traversable to the aggregate
44
     *
45
     * @param TraversableNodeInterface $node
46
     *
47
     * @throws NodalFlowException
48
     *
49
     * @return $this
50
     */
51
    public function addTraversable(TraversableNodeInterface $node)
52
    {
53
        $this->payload->add($node);
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get the traversable to traverse within the Flow
60
     *
61
     * @param mixed $param
62
     *
63
     * @return \Generator
64
     */
65
    public function getTraversable($param)
66
    {
67
        $value = null;
68
        /** @var $nodes TraversableNodeInterface[] */
69
        $nodes = $this->payload->getNodes();
70
        foreach ($nodes as $node) {
71
            $returnVal = $node->isReturningVal();
72
            $nodeStats = &$node->getCarrier()->getFlowMap()->getNodeStat($node->getId());
73
            foreach ($node->getTraversable($param) as $value) {
74
                ++$nodeStats['num_iterate'];
75
                if ($returnVal) {
76
                    yield $value;
77
                    continue;
78
                }
79
80
                yield $param;
81
            }
82
83
            ++$nodeStats['num_exec'];
84
            if ($returnVal) {
85
                // since this node is returning something
86
                // we will pass its last yield to the next
87
                // traversable. It will be up to him to
88
                // do whatever is necessary with it, including
89
                // nothing
90
                $param = $value;
91
            }
92
        }
93
    }
94
}
95