Completed
Pull Request — master (#40)
by
unknown
24:33
created

Fork::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace League\Pipeline;
4
5
6
class Fork implements ForkInterface
7
{
8
    /**
9
     * @var Pipeline
10
     */
11
    private $parent;
12
13
    /**
14
     * @var array callable
15
     */
16
    protected $forks = [];
17
18
    /**
19
     * @var callable
20
     */
21
    protected $resolver;
22
23
    /**
24
     * Fork constructor.
25
     * @param callable|null $resolver
26
     * @param array $forks
27
     */
28
    public function __construct(callable $resolver = null, $forks = [])
29
    {
30
        $this->resolver = $resolver;
31
        $this->forks = $forks;
32
    }
33
34
    public function setForks($forks)
35
    {
36
        $this->forks = $forks;
37
    }
38
39
    public function pipeline(Pipeline $pipeline)
40
    {
41
        $this->parent = $pipeline;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function join(callable $resolver = null)
48
    {
49
        if($resolver != null)
50
        {
51
            $this->resolver = $resolver;
52
        }
53
54
        return $this->parent;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function disjoin(string $tag, callable $stage = null)
61
    {
62
        $pipeline = new DisjointAwarePipeline($this);
63
64
        if($stage != null)
65
        {
66
            $pipeline = $pipeline->pipe($stage);
67
        }
68
69
        $this->forks[$tag] = $pipeline;
70
        return $pipeline;
71
    }
72
73
    /**
74
     * Chooses a fork or short-circuits based on $resolver
75
     *
76
     * @param mixed $payload
77
     * @return mixed
78
     */
79
    public function __invoke($payload)
80
    {
81
        $flowTo = call_user_func($this->resolver, $payload);
82
        if($flowTo === false) return $payload;
83
        $result = $this->forks[$flowTo]->process($payload);
84
85
        return $result;
86
    }
87
}
88