Skip to content

Commit

Permalink
V2 (#2)
Browse files Browse the repository at this point in the history
* Stage implementos to abstract (#1)

* rename method stage to handle (#3)

* Options (#4)
  • Loading branch information
cesargb authored Oct 3, 2022
1 parent 59e267c commit 82ed9db
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 23 deletions.
43 changes: 38 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions src/PipeLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions src/PipeLineOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Descom\Pipeline;

class PipeLineOptions
{
private array $options = [];

public function __construct(array $options)
{
foreach ($options as $key => $value) {
$this->options[$key] = $value;
}
}

public function get(string $key)
{
return $this->options[$key] ?? null;
}
}
20 changes: 18 additions & 2 deletions src/Stage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@

namespace Descom\Pipeline;

interface Stage
use Descom\Pipeline\Test\PipelineTest;

abstract class Stage
{
private PipeLineOptions $options;

/**
* Process the payload.
*
* @param mixed $payload
*
* @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);
}
}
54 changes: 54 additions & 0 deletions tests/ArgumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Descom\Pipeline\Test;

use Descom\Pipeline\Tests\Support\ArgumentPipeline;
use Descom\Pipeline\Tests\Support\ArgumentPipelineSample;
use Descom\Pipeline\Tests\Support\ArgumentStage;
use Descom\Pipeline\Tests\Support\DoubleArgumentStage;
use PHPUnit\Framework\TestCase;

class ArgumentTest extends TestCase
{
public function testPipelineWithArguments()
{
ArgumentPipeline::getInstance()->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)
);
}
}
6 changes: 3 additions & 3 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
9 changes: 9 additions & 0 deletions tests/Support/ArgumentPipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Descom\Pipeline\Tests\Support;

use Descom\Pipeline\PipeLine;

class ArgumentPipeline extends PipeLine
{
}
9 changes: 9 additions & 0 deletions tests/Support/ArgumentPipelineSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Descom\Pipeline\Tests\Support;

use Descom\Pipeline\PipeLine;

class ArgumentPipelineSample extends PipeLine
{
}
25 changes: 25 additions & 0 deletions tests/Support/ArgumentStage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Descom\Pipeline\Tests\Support;

use Descom\Pipeline\Stage;

class ArgumentStage extends Stage
{
public function handle($payload)
{
$priceWithDiscount = $this->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);
}
}
15 changes: 15 additions & 0 deletions tests/Support/DoubleArgumentStage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Descom\Pipeline\Tests\Support;

use Descom\Pipeline\Stage;

class DoubleArgumentStage extends Stage
{
public function handle($payload): int
{
$twiceDouble = $this->option('twiceDouble') ?? 1;

return $payload * pow(2, $twiceDouble);
}
}
4 changes: 2 additions & 2 deletions tests/Support/DoubleStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 82ed9db

Please sign in to comment.