Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Symfony 5 Factory/Abstract Class Current Code
<?php
namespace App\Library\Handler;
use App\Library\Test\Client;
use App\Library\Parser\Parser;
abstract class AbstractHandler
{
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $email;
/**
* @var string
*/
protected $password;
/**
* @var bool
*/
protected $delete;
/**
* @var Parser
*/
protected $parser;
/**
* @var Client
*/
private $client;
public function __construct(Client $client, Parser $parser)
{
$this->parser = $parser;
$this->client = $client;
}
abstract public function handle(string $path, string $email, string $password, bool $delete);
}
<?php
namespace App\Library\Handler;
use App\Library\Test\Client;
use App\Library\Parser\Parser;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HandlerFactory
{
/**
* @var ImportHandler|null
*/
protected static $importHandler;
/**
* @var ScheduleHandler|null
*/
protected static $scheduleHandler;
public static function createHandlers(Client $client, Parser $parser)
{
self::$importHandler = new ImportHandler($client, $parser);
self::$scheduleHandler = new ScheduleHandler($client, $parser);
}
public static function buildCommand(InputInterface $input, OutputInterface $output)
{
$command = $input->getArgument('type');
$email = $input->getOption('email');
$password = $input->getOption('password');
$delete = $input->getOption('delete');
$path = $input->getArgument('csv');
switch($command) {
case 'import':
self::$importHandler->handle($path, $email, $password, $delete);
return self::$importHandler;
break;
case 'schedule':
$start = $input->getOption('start');
$end = $input->getOption('end');
self::$scheduleHandler->handle($path, $email, $password, $delete, $start, $end);
return self::$scheduleHandler;
break;
default:
break;
}
}
}
<?php
namespace App\Library\Handler;
class ImportHandler extends AbstractHandler
{
public function handle(string $path, string $email, string $password, bool $delete)
{
$this->parser->validate($path);
}
}
App\Library\Handler\HandlerFactory:
arguments: ['@App\Library\Handler\ImportHandler', '@App\Library\Handler\ScheduleHandler']
App\Library\Handler\AbstractHandler:
abstract: true
arguments: ['@App\Library\Test\Client', '@App\Library\Parser\Parser']
App\Library\Handler\ImportHandler:
factory: ['App\Library\Handler\HandlerFactory', 'createHandlers']
App\Library\Handler\ScheduleHandler:
factory: ['App\Library\Handler\HandlerFactory', 'createHandlers']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment