Created
April 19, 2020 14:56
-
-
Save Raistlfiren/a511f4cc4280db7a3a8c29dac6ffa6c9 to your computer and use it in GitHub Desktop.
Symfony 5 Factory/Abstract Class Current Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Library\Handler; | |
class ImportHandler extends AbstractHandler | |
{ | |
public function handle(string $path, string $email, string $password, bool $delete) | |
{ | |
$this->parser->validate($path); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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