PayloadNodeFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 5
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;
11
12
use Closure;
13
use fab2s\NodalFlow\Flows\FlowInterface;
14
use fab2s\NodalFlow\Nodes\BranchNode;
15
use fab2s\NodalFlow\Nodes\CallableNode;
16
use fab2s\NodalFlow\Nodes\ClosureNode;
17
use fab2s\NodalFlow\Nodes\PayloadNodeFactoryInterface;
18
use fab2s\NodalFlow\Nodes\PayloadNodeInterface;
19
20
/**
21
 * class PayloadNodeFactory
22
 */
23
class PayloadNodeFactory implements PayloadNodeFactoryInterface
24
{
25
    /**
26
     * Instantiate the proper Payload Node for the payload
27
     *
28
     * @param mixed $payload
29
     * @param bool  $isAReturningVal
30
     * @param bool  $isATraversable
31
     *
32
     * @throws NodalFlowException
33
     *
34
     * @return PayloadNodeInterface
35
     */
36
    public static function create($payload, bool $isAReturningVal, bool $isATraversable = false): PayloadNodeInterface
37
    {
38
        if (\is_array($payload) || \is_string($payload)) {
39
            return new CallableNode($payload, $isAReturningVal, $isATraversable);
40
        }
41
42
        if ($payload instanceof Closure) {
43
            // distinguishing Closures actually is surrealistic
44
            return new ClosureNode($payload, $isAReturningVal, $isATraversable);
45
        }
46
47
        if ($payload instanceof FlowInterface) {
48
            return new BranchNode($payload, $isAReturningVal);
49
        }
50
51
        throw new NodalFlowException('Payload not supported, must be Callable or Flow');
52
    }
53
}
54