Completed
Pull Request — master (#39)
by
unknown
24:08
created

Pipeline::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace League\Pipeline;
4
5
use InvalidArgumentException;
6
7
class Pipeline implements PipelineInterface
8
{
9
    /**
10
     * @var callable[]
11
     */
12
    private $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 18
    public function __construct(array $stages = [], ProcessorInterface $processor = null)
28
    {
29 18
        foreach ($stages as $stage) {
30 6
            if (false === is_callable($stage)) {
31 2
                throw new InvalidArgumentException('All stages should be callable.');
32
            }
33 16
        }
34
35 16
        $this->stages = $stages;
36 16
        $this->processor = $processor ?: new FingersCrossedProcessor;
37 16
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 8
    public function pipe(callable $stage)
43
    {
44 8
        $pipeline = clone $this;
45 8
        $pipeline->stages[] = $stage;
46
47 8
        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 4
     * @param callable|null $resolver
66
     *
67 4
     * @return Fork
68
     */
69
    public function fork(callable $resolver = null)
70
    {
71
        $fork = new Fork($this, $resolver);
72
        return $fork;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function __invoke($payload)
79
    {
80
        return $this->process($payload);
81
    }
82
}
83