mardi 12 février 2019

Best strategy to use pipeline pattern for testing and method insulation in PHP

My pipeline pattern for upload foto:

    // Define the pipeline stages
    $pipeline = (new Pipeline\Pipeline(new Pipeline\InterruptiblePayloadProcessor))
        ->pipe(new \component\Product\Foto\GenerateName)
        ->pipe(new \component\Product\Foto\Upload)
        ->pipe(new \component\Product\Foto\Resize)
        ->pipe(new \component\Product\Foto\Save)
        ->pipe(new \component\Product\Foto\UpdateDefault);

    // The payload is an object that's passed between stages
    $payload = new Pipeline\Payload($_POST, ['files' => $_FILES]);
    // Run the pipeline
    $payload = $pipeline->process($payload);

I want use some of classes in other place in code (or test it):

$class = new \component\Product\Foto\GenerateName();
$res = $class->action('ABC');

Problem is that those classes use config instance example:

public function fotoFileExist($filename, $ext)
{
    $foto_subdirs = C::inst()->get('product_foto.subdirs');
    // ...
}

What is best elegant way to deal with it:

A) Dependency Injection. Add constructor to class for ConfigInterface

public function __construct(\core\ConfigInterface $c)
{
    $this->conf = $c;
}

..->pipe(new \component\Product\Foto\GenerateName(C::inst()))
..->pipe(new \component\Product\Foto\Upload(C::inst()))
...

Is this elegent so many repeats of C::inst() ?

B) Add Config obj to Pipeline:

$payload = new Pipeline\Payload($_POST, ['files' => $_FILES], ['config' => C::inst()]);

C) Use only single array data from config example:

$config = C::inst()->get('product_foto);
..->pipe(new \component\Product\Foto\GenerateName($config))
..->pipe(new \component\Product\Foto\Upload($config))
...

Aucun commentaire:

Enregistrer un commentaire