My implementation of the Pipeline pattern based upon this article.
In my case, I needed to download data from an API, run it through a Service Object, then run through another Service Object. It is mainly used to execute code through a series of stages and then yeild a final value but it can also be used to make complex code more readable.
$ composer require jamielsharief/pipeline
Create a Handler for each stage in the pipeline
class DownloadData implements HandlerInterface
{
/**
* Handler code
*/
public function handle($payload = null)
{
}
}
Create the pipeline
$pipeline = (new Pipeline(new DownloadData()))
->add(new CleanUpData())
->add(new ConvertToJson())
->add(new AddToDatabase());
When you want to execute it call the dispatch
method and pass any payload.
$pipeline->dispatch('8a967e35-f818-43e9-931a-f7f5b6760793');