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

Pipeline::__construct()   A

↳ Parent: Pipeline

Complexity

Conditions 3
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 10
Code Lines 5

Code Coverage

Tests 7
CRAP Score 3

Importance

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