44
55namespace Saloon \Helpers ;
66
7+ use Saloon \Data \Pipe ;
78use Saloon \Exceptions \DuplicatePipeNameException ;
9+ use Saloon \Contracts \Pipeline as PipelineContract ;
810
9- class Pipeline
11+ class Pipeline implements PipelineContract
1012{
1113 /**
1214 * The pipes in the pipeline.
1315 *
14- * @var array
16+ * @var array<\Saloon\Data\Pipe>
1517 */
1618 protected array $ pipes = [];
1719
1820 /**
19- * The named pipes that have been added.
21+ * Add a pipe to the pipeline
2022 *
21- * @var array
22- */
23- protected array $ namedPipes = [];
24-
25- /**
26- * Add a pipe to the pipeline.
27- *
28- * @param callable $pipe
23+ * @param callable $callable
2924 * @param bool $prepend
3025 * @param string|null $name
3126 * @return $this
3227 * @throws \Saloon\Exceptions\DuplicatePipeNameException
3328 */
34- public function pipe (callable $ pipe , bool $ prepend = false , ?string $ name = null ): static
29+ public function pipe (callable $ callable , bool $ prepend = false , ?string $ name = null ): static
3530 {
36- if ($ prepend === true ) {
37- array_unshift ($ this ->pipes , $ pipe );
38- } else {
39- $ this ->pipes [] = $ pipe ;
40- }
31+ $ pipe = new Pipe ($ callable , $ name );
4132
42- if (! is_null ($ name )) {
43- in_array ( $ name , $ this -> namedPipes , true ) ? throw new DuplicatePipeNameException ($ name ) : $ this -> namedPipes [] = $ name ;
33+ if (is_string ( $ name ) && $ this -> pipeExists ($ name )) {
34+ throw new DuplicatePipeNameException ($ name );
4435 }
4536
37+ $ prepend === true
38+ ? array_unshift ($ this ->pipes , $ pipe )
39+ : $ this ->pipes [] = $ pipe ;
40+
4641 return $ this ;
4742 }
4843
@@ -55,7 +50,7 @@ public function pipe(callable $pipe, bool $prepend = false, ?string $name = null
5550 public function process (mixed $ payload ): mixed
5651 {
5752 foreach ($ this ->pipes as $ pipe ) {
58- $ payload = $ pipe( $ payload );
53+ $ payload = call_user_func ( $ pipe-> callable , $ payload );
5954 }
6055
6156 return $ payload ;
@@ -66,21 +61,46 @@ public function process(mixed $payload): mixed
6661 *
6762 * @param array $pipes
6863 * @return $this
64+ * @throws \Saloon\Exceptions\DuplicatePipeNameException
6965 */
7066 public function setPipes (array $ pipes ): static
7167 {
72- $ this ->pipes = $ pipes ;
68+ $ this ->pipes = [];
69+
70+ // Loop through each of the pipes and manually add each pipe
71+ // so we can check if the name already exists.
72+
73+ foreach ($ pipes as $ pipe ) {
74+ $ this ->pipe ($ pipe ->callable , false , $ pipe ->name );
75+ }
7376
7477 return $ this ;
7578 }
7679
7780 /**
7881 * Get all the pipes in the pipeline
7982 *
80- * @return array
83+ * @return array<\Saloon\Data\Pipe>
8184 */
8285 public function getPipes (): array
8386 {
8487 return $ this ->pipes ;
8588 }
89+
90+ /**
91+ * Check if a given pipe exists for a name
92+ *
93+ * @param string $name
94+ * @return bool
95+ */
96+ protected function pipeExists (string $ name ): bool
97+ {
98+ foreach ($ this ->pipes as $ pipe ) {
99+ if ($ pipe ->name === $ name ) {
100+ return true ;
101+ }
102+ }
103+
104+ return false ;
105+ }
86106}
0 commit comments