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

Pipeline   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A pipe() 0 7 1
A process() 0 4 1
A fork() 0 5 1
A __invoke() 0 4 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
    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