ClosureNode   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 43
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getTraversable() 0 5 2
A exec() 0 5 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 Closure;
13
use fab2s\NodalFlow\NodalFlowException;
14
use Generator;
15
16
/**
17
 * Class ClosureNode
18
 */
19
class ClosureNode extends CallableNode
20
{
21
    /**
22
     * Instantiate a Closure Node
23
     *
24
     * @param Closure $payload
25
     * @param bool    $isAReturningVal
26
     * @param bool    $isATraversable
27
     *
28
     * @throws NodalFlowException
29
     */
30
    public function __construct(Closure $payload, bool $isAReturningVal, bool $isATraversable = false)
31
    {
32
        parent::__construct($payload, $isAReturningVal, $isATraversable);
33
    }
34
35
    /**
36
     * Get this Node's Traversable (payload must be consistent for the usage)
37
     *
38
     * @param mixed $param
39
     *
40
     * @return Generator
41
     */
42
    public function getTraversable($param = null): iterable
43
    {
44
        $callable = $this->payload;
45
        foreach ($callable($param) as $value) {
46
            yield $value;
47
        }
48
    }
49
50
    /**
51
     * Execute this Node (payload must be consistent for the usage)
52
     *
53
     * @param mixed|null $param
54
     *
55
     * @return mixed
56
     */
57
    public function exec($param = null)
58
    {
59
        $callable = $this->payload;
60
61
        return $callable($param);
62
    }
63
}
64