Completed
Push — master ( 0228a6...5da3c1 )
by Fabrice
13:35
created

AggregateNode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 86
rs 10
c 4
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addTraversable() 0 6 1
B getTraversable() 0 26 5
A exec() 0 4 1
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
    }
40
41
    /**
42
     * Add a traversable to the aggregate
43
     *
44
     * @param TraversableNodeInterface $node
45
     *
46
     * @throws NodalFlowException
47
     *
48
     * @return $this
49
     */
50
    public function addTraversable(TraversableNodeInterface $node)
51
    {
52
        $this->payload->add($node);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get the traversable to traverse within the Flow
59
     *
60
     * @param mixed $param
61
     *
62
     * @return \Generator
63
     */
64
    public function getTraversable($param)
65
    {
66
        $value = null;
67
        /** @var $nodes TraversableNodeInterface[] */
68
        $nodes = $this->payload->getNodes();
69
        foreach ($nodes as $node) {
70
            $returnVal = $node->isReturningVal();
71
            foreach ($node->getTraversable($param) as $value) {
72
                if ($returnVal) {
73
                    yield $value;
74
                    continue;
75
                }
76
77
                yield $param;
78
            }
79
80
            if ($returnVal) {
81
                // since this node is returning something
82
                // we will pass its last yield to the next
83
                // traversable. It will be up to him to
84
                // do whatever is necessary with it, including
85
                // nothing
86
                $param = $value;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $param. This often makes code more readable.
Loading history...
87
            }
88
        }
89
    }
90
91
    /**
92
     * Execute the BranchNode
93
     *
94
     * @param mixed $param
95
     *
96
     * @throws NodalFlowException
97
     *
98
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
99
     */
100
    public function exec($param)
101
    {
102
        throw new NodalFlowException('AggregateNode cannot be executed, use getTraversable to iterate instead');
103
    }
104
}
105