|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Component\Runtime\Workflow; |
| 6 | + |
| 7 | +use Kiboko\Component\Runtime\Pipeline\Console as PipelineConsoleRuntime; |
| 8 | +use Kiboko\Component\Runtime\Pipeline\PipelineRuntimeInterface; |
| 9 | +use Kiboko\Component\State; |
| 10 | +use Kiboko\Contract\Pipeline\ExtractorInterface; |
| 11 | +use Kiboko\Contract\Pipeline\LoaderInterface; |
| 12 | +use Kiboko\Contract\Pipeline\PipelineInterface; |
| 13 | +use Kiboko\Contract\Pipeline\RejectionInterface; |
| 14 | +use Kiboko\Contract\Pipeline\StateInterface; |
| 15 | +use Kiboko\Contract\Pipeline\TransformerInterface; |
| 16 | +use Kiboko\Contract\Pipeline\WalkableInterface; |
| 17 | +use Symfony\Component\Console\Output\ConsoleOutput; |
| 18 | + |
| 19 | +class PipelineProxy implements PipelineRuntimeInterface |
| 20 | +{ |
| 21 | + /** @var list<callable> */ |
| 22 | + private array $queuedCalls = []; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + callable $factory, |
| 26 | + private readonly ConsoleOutput $output, |
| 27 | + private readonly PipelineInterface&WalkableInterface $pipeline, |
| 28 | + private readonly State\StateOutput\Workflow $state, |
| 29 | + private readonly string $filename, |
| 30 | + ) { |
| 31 | + $this->queuedCalls[] = static function (PipelineConsoleRuntime $runtime) use ($factory): void { |
| 32 | + $factory($runtime); |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + public function extract( |
| 37 | + ExtractorInterface $extractor, |
| 38 | + RejectionInterface $rejection, |
| 39 | + StateInterface $state, |
| 40 | + ): self { |
| 41 | + $this->queuedCalls[] = static function (PipelineConsoleRuntime $runtime) use ($extractor, $rejection, $state): void { |
| 42 | + $runtime->extract($extractor, $rejection, $state); |
| 43 | + }; |
| 44 | + |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + public function transform( |
| 49 | + TransformerInterface $transformer, |
| 50 | + RejectionInterface $rejection, |
| 51 | + StateInterface $state, |
| 52 | + ): self { |
| 53 | + $this->queuedCalls[] = static function (PipelineConsoleRuntime $runtime) use ($transformer, $rejection, $state): void { |
| 54 | + $runtime->transform($transformer, $rejection, $state); |
| 55 | + }; |
| 56 | + |
| 57 | + return $this; |
| 58 | + } |
| 59 | + |
| 60 | + public function load( |
| 61 | + LoaderInterface $loader, |
| 62 | + RejectionInterface $rejection, |
| 63 | + StateInterface $state, |
| 64 | + ): self { |
| 65 | + $this->queuedCalls[] = static function (PipelineConsoleRuntime $runtime) use ($loader, $rejection, $state): void { |
| 66 | + $runtime->load($loader, $rejection, $state); |
| 67 | + }; |
| 68 | + |
| 69 | + return $this; |
| 70 | + } |
| 71 | + |
| 72 | + public function run(int $interval = 1000): int |
| 73 | + { |
| 74 | + $runtime = new PipelineConsoleRuntime($this->output, $this->pipeline, $this->state->withPipeline($this->filename)); |
| 75 | + |
| 76 | + foreach ($this->queuedCalls as $queuedCall) { |
| 77 | + $queuedCall($this); |
| 78 | + } |
| 79 | + |
| 80 | + $this->queuedCalls = []; |
| 81 | + |
| 82 | + return $runtime->run($interval); |
| 83 | + } |
| 84 | +} |
0 commit comments