Passed
Push — master ( 609c13...ddc20e )
by Fabrice
01:41
created

ControlNode   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A exec() 0 13 3
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
14
/**
15
 * Class ControlNode
16
 */
17
class ControlNode extends PayloadNodeAbstract implements ExecNodeInterface
18
{
19
    /**
20
     * Instantiate a Callable Node
21
     *
22
     * @param callable $payload
23
     * @param bool     $isAReturningVal
24
     * @param bool     $isATraversable
25
     *
26
     * @throws NodalFlowException
27
     */
28
    public function __construct(callable $payload, $isAReturningVal = true, $isATraversable = false)
29
    {
30
        // since a qualifier is supposed to only control the flow, not to alter it,
31
        // we will force it to return a value and to be a scalar node
32
        parent::__construct($payload, true, false);
33
    }
34
35
    /**
36
     * The ControlNode's payload interface is simple :
37
     *      - return false to break
38
     *      - return true to continue
39
     *      - return void|null (whatever) to proceed with the flow
40
     *
41
     * @param mixed $param
42
     *
43
     * @return mixed
44
     */
45
    public function exec($param)
46
    {
47
        switch (\call_user_func($this->payload, $param)) {
48
            case false:
49
                $this->carrier->continueFlow();
50
                break;
51
            case true:
52
                $this->carrier->breakFlow();
53
                break;
54
        }
55
56
        return $param;
57
    }
58
}
59