diff --git a/README.md b/README.md index 2eec085..01ab04d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Pipeline to plugins [![tests](https://github.com/descom-es/pipeline/actions/workflows/test.yml/badge.svg)](https://github.com/descom-es/pipeline/actions/workflows/test.yml) -[![analyse](https://github.com/descom-es/pipeline/actions/workflows/analyse.yml/badge.svg)](https://github.com/descom-es/pipeline/actions/workflows/analyse.yml) +[![analyze](https://github.com/descom-es/pipeline/actions/workflows/analyse.yml/badge.svg)](https://github.com/descom-es/pipeline/actions/workflows/analyse.yml) [![styles](https://github.com/descom-es/pipeline/actions/workflows/fix_style.yml/badge.svg)](https://github.com/descom-es/pipeline/actions/workflows/fix_style.yml) ## Install @@ -14,13 +14,12 @@ composer require descom/pipeline ## Usage - ### Create Stages Samples: - [DoubleStage](tests/Support/DoubleStage.php) -- [AddStage](tests/Support/AddStage.php) +- [IncreaseStage](tests/Support/IncreaseStage.php) ### Create Pipeline @@ -34,13 +33,47 @@ class SamplePipeline extends PipeLine ### Configure plugin to add Stages ```php - SamplePipeline::getInstance()->pipe(new DoubleStage())->pipe(new AddStage()); + SamplePipeline::getInstance() + ->pipe(new DoubleStage()) + ->pipe(new IncreaseStage()); ``` ### Process pipeline to transform data in core ```php - $payload = SamplePipeline::getInstance()->process($payload); + $payload = 10; + + $payload = SamplePipeline::getInstance() + ->process($payload); // return 21 (10 * 2) + 1 +``` + +### Options + +Perhaps you need to add options to the stages, for example, the number of times to double the value. +You can call method `with` to add options to the stages. + +```php + $payload = 10; + + $payload = SamplePipeline::getInstance() + ->process($payload, [ + 'twiceDouble' => 3, + 'twiceIncrease' => 2 + ]); // return 82 (10 * 2 ^ 3) + (1 + 1) +``` + +You can define `DoubleStage` as: + +```php +class DoubleStage extends Stage +{ + public function handle($payload): int + { + $twiceDouble = $this->option('twiceDouble') ?? 1; + + return $payload * pow(2, $twiceDouble); + } +} ``` ## Testing diff --git a/src/PipeLine.php b/src/PipeLine.php index 163ce5e..cbdb437 100644 --- a/src/PipeLine.php +++ b/src/PipeLine.php @@ -28,22 +28,19 @@ public function pipe(Stage $stage): static return $this; } - public function process($payload) - { - return $this->processStages($payload); - } - - private function processStages($payload) + public function process($payload, array $options = []) { foreach ($this->stages as $stage) { - $payload = $this->processStage($stage, $payload); + $payload = $this->processStage($stage, $payload, $options); } return $payload; } - private function processStage($stage, $payload) + private function processStage(Stage $stage, $payload, $options) { - return $stage->__invoke($payload); + $stage->options($options); + + return $stage->handle($payload); } } diff --git a/src/PipeLineOptions.php b/src/PipeLineOptions.php new file mode 100644 index 0000000..7f707b4 --- /dev/null +++ b/src/PipeLineOptions.php @@ -0,0 +1,20 @@ + $value) { + $this->options[$key] = $value; + } + } + + public function get(string $key) + { + return $this->options[$key] ?? null; + } +} diff --git a/src/Stage.php b/src/Stage.php index adb4783..7f49cc9 100644 --- a/src/Stage.php +++ b/src/Stage.php @@ -2,8 +2,12 @@ namespace Descom\Pipeline; -interface Stage +use Descom\Pipeline\Test\PipelineTest; + +abstract class Stage { + private PipeLineOptions $options; + /** * Process the payload. * @@ -11,5 +15,17 @@ interface Stage * * @return mixed */ - public function __invoke($payload); + abstract public function handle($payload); + + public function options(array $options): static + { + $this->options = new PipeLineOptions($options); + + return $this; + } + + protected function option(string $key) + { + return $this->options->get($key); + } } diff --git a/tests/ArgumentTest.php b/tests/ArgumentTest.php new file mode 100644 index 0000000..c7a1852 --- /dev/null +++ b/tests/ArgumentTest.php @@ -0,0 +1,54 @@ +pipe(new ArgumentStage()); + + $this->assertEquals( + 9.35, + ArgumentPipeline::getInstance() + ->process(10, [ + 'discountPercent' => 15, + 'taxPercent' => 10, + ]) + ); + + $this->assertEquals( + 9.2, + ArgumentPipeline::getInstance() + ->process(10, [ + 'discountPercent' => 20, + 'taxPercent' => 15, + ]) + ); + } + + public function testPipelineWithArgumentsSample() + { + ArgumentPipelineSample::getInstance()->pipe(new DoubleArgumentStage()); + + $this->assertEquals( + 80, + ArgumentPipelineSample::getInstance() + ->process(10, [ + 'twiceDouble' => 3 + ]) + ); + + $this->assertEquals( + 20, + ArgumentPipelineSample::getInstance() + ->process(10) + ); + } +} diff --git a/tests/PipelineTest.php b/tests/PipelineTest.php index 4a7f178..f4b621a 100644 --- a/tests/PipelineTest.php +++ b/tests/PipelineTest.php @@ -2,7 +2,7 @@ namespace Descom\Pipeline\Test; -use Descom\Pipeline\Tests\Support\AddStage; +use Descom\Pipeline\Tests\Support\IncreaseStage; use Descom\Pipeline\Tests\Support\DoubleStage; use Descom\Pipeline\Tests\Support\OnePipeline; use Descom\Pipeline\Tests\Support\ThreePipeline; @@ -22,8 +22,8 @@ public function testPipeline() private function injections() { - OnePipeline::getInstance()->pipe(new DoubleStage())->pipe(new AddStage()); + OnePipeline::getInstance()->pipe(new DoubleStage())->pipe(new IncreaseStage()); - TwoPipeline::getInstance()->pipe(new AddStage())->pipe(new DoubleStage()); + TwoPipeline::getInstance()->pipe(new IncreaseStage())->pipe(new DoubleStage()); } } diff --git a/tests/Support/ArgumentPipeline.php b/tests/Support/ArgumentPipeline.php new file mode 100644 index 0000000..57bc66e --- /dev/null +++ b/tests/Support/ArgumentPipeline.php @@ -0,0 +1,9 @@ +applyDiscount($payload); + + return round($this->applyTax($priceWithDiscount), 2); + } + + private function applyDiscount($price) + { + return $price * (1 - $this->option('discountPercent') / 100); + } + + private function applyTax($price) + { + return $price * (1 + $this->option('taxPercent') / 100); + } +} diff --git a/tests/Support/DoubleArgumentStage.php b/tests/Support/DoubleArgumentStage.php new file mode 100644 index 0000000..d729a54 --- /dev/null +++ b/tests/Support/DoubleArgumentStage.php @@ -0,0 +1,15 @@ +option('twiceDouble') ?? 1; + + return $payload * pow(2, $twiceDouble); + } +} diff --git a/tests/Support/DoubleStage.php b/tests/Support/DoubleStage.php index 2fa2b2b..cca47b2 100644 --- a/tests/Support/DoubleStage.php +++ b/tests/Support/DoubleStage.php @@ -4,9 +4,9 @@ use Descom\Pipeline\Stage; -class DoubleStage implements Stage +class DoubleStage extends Stage { - public function __invoke($payload) + public function handle($payload) { return $payload * 2; } diff --git a/tests/Support/AddStage.php b/tests/Support/IncreaseStage.php similarity index 63% rename from tests/Support/AddStage.php rename to tests/Support/IncreaseStage.php index a5260ed..5048742 100644 --- a/tests/Support/AddStage.php +++ b/tests/Support/IncreaseStage.php @@ -4,9 +4,9 @@ use Descom\Pipeline\Stage; -class AddStage implements Stage +class IncreaseStage extends Stage { - public function __invoke($payload) + public function handle($payload) { return $payload + 1; }