Completed
Push — master ( 783c9c...7d0a0b )
by Sébastien
03:32
created

Pipeline   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A __clone() 0 3 1
A outlet() 0 6 1
A setPipes() 0 6 1
A send() 0 8 2
A pipe() 0 6 1
A __invoke() 0 3 1
A prepend() 0 6 1
A clear() 0 3 1
1
<?php
2
3
namespace Bdf\Pipeline;
4
5
use Bdf\Pipeline\CallableFactory\StackCallableFactory;
6
7
/**
8
 * Pipeline
9
 *
10
 * @author Sébastien Tanneux
11
 */
12
final class Pipeline
13
{
14
    /**
15
     * The callable factory
16
     *
17
     * @var CallableFactoryInterface
18
     */
19
    private $factory;
20
21
    /**
22
     * The pipes
23
     *
24
     * @var callable[]
25
     */
26
    private $pipes = [];
27
28
    /**
29
     * The destination of the last pipe
30
     *
31
     * @var callable
32
     */
33
    private $outlet;
34
35
    /**
36
     * The built callable
37
     *
38
     * @var callable
39
     */
40
    private $callable;
41
42
    /**
43
     * Pipeline constructor.
44
     *
45
     * @param CallableFactoryInterface $factory
46
     */
47
    public function __construct(CallableFactoryInterface $factory = null)
48
    {
49
        $this->factory = $factory ?: new StackCallableFactory();
50
    }
51
52
    /**
53
     * Set the pipes
54
     *
55
     * @param callable[] $pipes
56
     *
57
     * @return $this
58
     */
59
    public function setPipes(array $pipes)
60
    {
61
        $this->clear();
62
        $this->pipes = $pipes;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Add a pipe at the beginning of the chain
69
     *
70
     * @param callable $first
71
     *
72
     * @return $this
73
     */
74
    public function prepend($first)
75
    {
76
        $this->clear();
77
        array_unshift($this->pipes, $first);
78
79
        return $this;
80
    }
81
82
    /**
83
     * Add a pipe
84
     *
85
     * @param callable $last
86
     *
87
     * @return $this
88
     */
89
    public function pipe($last)
90
    {
91
        $this->clear();
92
        $this->pipes[] = $last;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Set the pipeline outlet
99
     *
100
     * @param callable $outlet
101
     *
102
     * @return $this
103
     */
104
    public function outlet(callable $outlet)
105
    {
106
        $this->clear();
107
        $this->outlet = $outlet;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Send the payload into the pipeline.
114
     *
115
     * @param array $payload
116
     *
117
     * @return mixed
118
     */
119
    public function send(...$payload)
120
    {
121
        if ($this->callable === null) {
122
            $this->callable = $this->factory->createCallable($this->pipes, $this->outlet);
123
        }
124
125
        $callable = $this->callable;
126
        return $callable(...$payload);
127
    }
128
129
    /**
130
     * Pipeline invokation
131
     *
132
     * @param array $payload
133
     *
134
     * @return mixed
135
     */
136
    public function __invoke(...$payload)
137
    {
138
        return $this->factory->callPipeline($this, $payload);
139
    }
140
141
    /**
142
     * Clear the callable
143
     */
144
    public function __clone()
145
    {
146
        $this->clear();
147
    }
148
149
    /**
150
     * Clear the callable
151
     */
152
    private function clear()
153
    {
154
        $this->callable = null;
155
    }
156
}
157