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

Fork   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 24%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 76
ccs 6
cts 25
cp 0.24
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A pipeline() 0 4 1
A join() 0 9 2
A disjoin() 0 12 2
A __invoke() 0 8 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
     *
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