Completed
Pull Request — master (#39)
by
unknown
25:42 queued 24:22
created

Fork::pipeline()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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