Completed
Push — master ( 297732...0f69fa )
by Frank
03:26
created

Pipeline::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 7
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 7
b 1
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
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
     * Constructor.
16
     *
17
     * @param callable[] $stages
18
     *
19
     * @throws InvalidArgumentException
20
     */
21 18
    public function __construct(array $stages = [])
22
    {
23 18
        foreach ($stages as $stage) {
24 6
            if (false === is_callable($stage)) {
25 2
                throw new InvalidArgumentException('All stages should be callable.');
26
            }
27 16
        }
28
29 16
        $this->stages = $stages;
30 16
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35 8
    public function pipe(callable $stage)
36
    {
37 8
        $pipeline = clone $this;
38 8
        $pipeline->stages[] = $stage;
39
40 8
        return $pipeline;
41
    }
42
43
    /**
44
     * Process the payload.
45
     *
46
     * @param $payload
47
     *
48
     * @return mixed
49
     */
50 10
    public function process($payload)
51
    {
52 10
        foreach ($this->stages as $stage) {
53 10
            $payload = call_user_func($stage, $payload);
54 10
        }
55
56 10
        return $payload;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 4
    public function __invoke($payload)
63
    {
64 4
        return $this->process($payload);
65
    }
66
}
67