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

Pipeline::fork()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace League\Pipeline;
4
5
use InvalidArgumentException;
6
7
class Pipeline implements PipelineInterface
8
{
9
    /**
10
     * @var callable[]
11
     */
12
    protected $stages = [];
13
14
    /**
15
     * @var ProcessorInterface
16
     */
17
    private $processor;
18
19
    /**
20
     * Constructor.
21
     *
22
     * @param callable[]         $stages
23
     * @param ProcessorInterface $processor
24
     *
25
     * @throws InvalidArgumentException
26
     */
27 20
    public function __construct(array $stages = [], ProcessorInterface $processor = null)
28
    {
29 20
        foreach ($stages as $stage) {
30 6
            if (false === is_callable($stage)) {
31 2
                throw new InvalidArgumentException('All stages should be callable.');
32
            }
33 18
        }
34
35 18
        $this->stages = $stages;
36 18
        $this->processor = $processor ?: new FingersCrossedProcessor;
37 18
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 10
    public function pipe(callable $stage)
43
    {
44 10
        $pipeline = clone $this;
45 10
        $pipeline->stages[] = $stage;
46
47 10
        return $pipeline;
48
    }
49
50
    /**
51
     * Process the payload.
52
     *
53
     * @param $payload
54
     *
55
     * @return mixed
56
     */
57 10
    public function process($payload)
58
    {
59 10
        return $this->processor->process($this->stages, $payload);
60
    }
61
62
    /**
63
     * Fork the pipeline
64
     *
65
     * @param callable|null $resolver
66
     *
67
     * @return Fork
68
     */
69 2
    public function fork(callable $resolver = null)
70
    {
71 2
        $fork = new Fork($resolver);
72 2
        $pipeline = $this->pipe($fork);
73 2
        $fork->pipeline($pipeline);
74 2
        return $fork;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 4
    public function __invoke($payload)
81
    {
82 4
        return $this->process($payload);
83
    }
84
}
85