CallableNode::getTraversable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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\NodalFlowException;
13
use Generator;
14
15
/**
16
 * Class CallableNode
17
 */
18
class CallableNode extends PayloadNodeAbstract implements TraversableNodeInterface, ExecNodeInterface
19
{
20
    /**
21
     * The underlying executable or traversable Payload
22
     *
23
     * @var callable
24
     */
25
    protected $payload;
26
27
    /**
28
     * Instantiate a Callable Node
29
     *
30
     * @param callable $payload
31
     * @param bool     $isAReturningVal
32
     * @param bool     $isATraversable
33
     *
34
     * @throws NodalFlowException
35
     */
36
    public function __construct(callable $payload, bool $isAReturningVal, bool $isATraversable = false)
37
    {
38
        parent::__construct($payload, $isAReturningVal, $isATraversable);
39
    }
40
41
    /**
42
     * Execute this node
43
     *
44
     * @param mixed|null $param
45
     *
46
     * @return mixed
47
     */
48
    public function exec($param = null)
49
    {
50
        return \call_user_func($this->payload, $param);
51
    }
52
53
    /**
54
     * Get this Node's Traversable
55
     *
56
     * @param mixed $param
57
     *
58
     * @return Generator
59
     */
60
    public function getTraversable($param = null): iterable
61
    {
62
        foreach (\call_user_func($this->payload, $param) as $value) {
63
            yield $value;
64
        }
65
    }
66
}
67