Completed
Pull Request — master (#40)
by
unknown
21:56
created

Fork::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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 pipeline(Pipeline $pipeline)
35
    {
36
        $this->parent = $pipeline;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function join(callable $resolver = null)
43
    {
44
        if($resolver != null)
45
        {
46
            $this->resolver = $resolver;
47
        }
48
49
        return $this->parent;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function disjoin(string $tag, callable $stage = null)
56
    {
57
        $pipeline = new DisjointAwarePipeline($this);
58
59
        if($stage != null)
60
        {
61
            $pipeline = $pipeline->pipe($stage);
62
        }
63
64
        $this->forks[$tag] = $pipeline;
65
        return $pipeline;
66
    }
67
68
    /**
69
     * Chooses a fork or short-circuits based on $resolver
70
     *
71
     * @param mixed $payload
72
     * @return mixed
73
     */
74
    public function __invoke($payload)
75
    {
76
        $flowTo = call_user_func($this->resolver, $payload);
77
        if($flowTo === false) return $payload;
78
        $result = $this->forks[$flowTo]->process($payload);
79
80
        return $result;
81
    }
82
}
83