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

Pipeline::pipe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 5
b 0
f 1
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
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