From 39d6fa4723bfe653ca42399a682ea1efd22bef58 Mon Sep 17 00:00:00 2001 From: Rick in t Veld Date: Thu, 25 Apr 2024 15:55:11 +0200 Subject: [PATCH] Implemented post hook actions --- config/services.yaml | 7 +++++ src/Action/Download/FileDownloadAction.php | 26 ++++++++++++++++ src/Action/FileSystem/CreateCsvFileAction.php | 30 +++++++++++++++++++ src/ActionHandler/ActionHandler.php | 14 +++++++-- src/Command/ExportCommand.php | 21 +++++-------- src/Dto/Aggregator/AggregateInterface.php | 8 ++--- src/Dto/Aggregator/AggregateRoot.php | 10 +++---- 7 files changed, 92 insertions(+), 24 deletions(-) create mode 100644 src/Action/Download/FileDownloadAction.php create mode 100644 src/Action/FileSystem/CreateCsvFileAction.php diff --git a/config/services.yaml b/config/services.yaml index 9753d6c..22cb38d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -59,6 +59,13 @@ services: '@App\Action\Order\History', ] + App\Command\ExportCommand: + arguments: + $postHookActions: [ + '@App\Action\FileSystem\CreateCsvFileAction', + '@App\Action\Download\FileDownloadAction' + ] + App\ActionHandler\ActionHandlerInterface $accountActionHandler: '@App\ActionHandler\AccountActionHandler' App\ActionHandler\ActionHandlerInterface $dailyDataActionHandler: '@App\ActionHandler\DailyDataActionHandler' App\ActionHandler\ActionHandlerInterface $dailyGainActionHandler: '@App\ActionHandler\DailyGainsActionHandler' diff --git a/src/Action/Download/FileDownloadAction.php b/src/Action/Download/FileDownloadAction.php new file mode 100644 index 0000000..0dec0cc --- /dev/null +++ b/src/Action/Download/FileDownloadAction.php @@ -0,0 +1,26 @@ +getData() instanceof File) === false) { + throw new \RuntimeException('Data should be an instance of class ' . File::class); + } + + ($this->fileDownloadManager)($aggregator->getData()); + } +} diff --git a/src/Action/FileSystem/CreateCsvFileAction.php b/src/Action/FileSystem/CreateCsvFileAction.php new file mode 100644 index 0000000..f2666c9 --- /dev/null +++ b/src/Action/FileSystem/CreateCsvFileAction.php @@ -0,0 +1,30 @@ +setFileName(sprintf('export_%d', (new \DateTime())->getTimestamp())); + + $csvFile->setContents( + $this->serializer->encode($aggregator->getData(), 'csv', ['csv_delimiter' => ',']) + ); + + $aggregator->setData($csvFile); + } +} diff --git a/src/ActionHandler/ActionHandler.php b/src/ActionHandler/ActionHandler.php index 0297f7f..6421d76 100644 --- a/src/ActionHandler/ActionHandler.php +++ b/src/ActionHandler/ActionHandler.php @@ -12,15 +12,25 @@ abstract class ActionHandler implements ActionHandlerInterface { /** - * @param iterable $actions + * @param array $actions */ public function __construct( - private readonly iterable $actions, + private array $actions, private readonly CircuitBreakerInterface $circuitBreaker, private readonly LoggerInterface $logger ) { } + public function preHook(ActionInterface $action): void + { + array_unshift($this->actions, $action); + } + + public function postHook(ActionInterface $action): void + { + array_push($this->actions, $action); + } + public function __invoke(AggregateInterface $aggregate): void { foreach ($this->actions as $action) { diff --git a/src/Command/ExportCommand.php b/src/Command/ExportCommand.php index 87f0fd5..ca5b849 100644 --- a/src/Command/ExportCommand.php +++ b/src/Command/ExportCommand.php @@ -4,11 +4,9 @@ namespace App\Command; +use App\ActionHandler\ActionHandler; use App\Dto\Aggregator\AggregateRoot; -use App\FileSystem\CsvFile; use App\Manager\ActionHandlerManager; -use App\Manager\FileDownloadManager; -use App\Serializer\Serializer; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; @@ -27,8 +25,7 @@ class ExportCommand extends Command public function __construct( private readonly ActionHandlerManager $actionHandlerManager, - private readonly FileDownloadManager $fileDownloadManager, - private readonly Serializer $serializer + private readonly array $postHookActions ) { parent::__construct(); } @@ -45,18 +42,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $aggregator = new AggregateRoot(); + /** @var ActionHandler $actionHandler */ $actionHandler = ($this->actionHandlerManager)($handler); - $actionHandler($aggregator); - $csvFile = new CsvFile(); - $csvFile->setFileName($handler); - $csvFile->setContents( - $this->serializer->encode($aggregator->getData(), 'csv', ['csv_delimiter' => ',']) - ); + foreach ($this->postHookActions as $action) { + $actionHandler->postHook($action); + } - ($this->fileDownloadManager)($csvFile); + $actionHandler($aggregator); - $io->success(sprintf('Finished downloading data to: %s', $csvFile->getFileName())); + $io->success('Finished downloading the data...'); return Command::SUCCESS; } diff --git a/src/Dto/Aggregator/AggregateInterface.php b/src/Dto/Aggregator/AggregateInterface.php index 4697cca..c631d85 100644 --- a/src/Dto/Aggregator/AggregateInterface.php +++ b/src/Dto/Aggregator/AggregateInterface.php @@ -18,15 +18,15 @@ public function getAccounts(): array; public function setAccounts(array $accounts): void; /** - * @return array + * @return mixed * @throws \Exception */ - public function getData(): array; + public function getData(): mixed; /** - * @param array $data + * @param mixed $data */ - public function setData(array $data): void; + public function setData(mixed $data): void; /** * @throws \Exception diff --git a/src/Dto/Aggregator/AggregateRoot.php b/src/Dto/Aggregator/AggregateRoot.php index 02bdb9a..f2d2b01 100644 --- a/src/Dto/Aggregator/AggregateRoot.php +++ b/src/Dto/Aggregator/AggregateRoot.php @@ -9,8 +9,8 @@ class AggregateRoot implements AggregateInterface /** @var array */ private array $accounts = []; - /** @var array */ - private array $data = []; + /** @var mixed */ + private mixed $data = null; private ?string $session; @@ -28,14 +28,14 @@ public function getAccounts(): array return $this->accounts; } - public function setData(array $data): void + public function setData(mixed $data): void { $this->data = $data; } - public function getData(): array + public function getData(): mixed { - if (empty($this->data)) { + if ($this->data === null) { throw new \Exception('No data found!'); }