Skip to content

Commit

Permalink
Implemented post hook actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick in t Veld committed Apr 25, 2024
1 parent 03e8f59 commit 39d6fa4
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 24 deletions.
7 changes: 7 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 26 additions & 0 deletions src/Action/Download/FileDownloadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Action\Download;

use App\Action\ActionInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\FileSystem\File;
use App\Manager\FileDownloadManager;

final class FileDownloadAction implements ActionInterface
{
public function __construct(private readonly FileDownloadManager $fileDownloadManager)
{
}

public function __invoke(AggregateInterface $aggregator): void
{
if (($aggregator->getData() instanceof File) === false) {
throw new \RuntimeException('Data should be an instance of class ' . File::class);
}

($this->fileDownloadManager)($aggregator->getData());
}
}
30 changes: 30 additions & 0 deletions src/Action/FileSystem/CreateCsvFileAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Action\FileSystem;

use App\Action\ActionInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\FileSystem\CsvFile;
use App\Serializer\Serializer;

class CreateCsvFileAction implements ActionInterface
{
public function __construct(private readonly Serializer $serializer)
{
}

public function __invoke(AggregateInterface $aggregator): void
{
$csvFile = new CsvFile();

$csvFile->setFileName(sprintf('export_%d', (new \DateTime())->getTimestamp()));

$csvFile->setContents(
$this->serializer->encode($aggregator->getData(), 'csv', ['csv_delimiter' => ','])
);

$aggregator->setData($csvFile);
}
}
14 changes: 12 additions & 2 deletions src/ActionHandler/ActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@
abstract class ActionHandler implements ActionHandlerInterface
{
/**
* @param iterable<ActionInterface> $actions
* @param array<ActionInterface> $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) {
Expand Down
21 changes: 8 additions & 13 deletions src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +25,7 @@ class ExportCommand extends Command

public function __construct(

Check failure on line 26 in src/Command/ExportCommand.php

View workflow job for this annotation

GitHub Actions / lint

Method App\Command\ExportCommand::__construct() has parameter $postHookActions with no value type specified in iterable type array.
private readonly ActionHandlerManager $actionHandlerManager,
private readonly FileDownloadManager $fileDownloadManager,
private readonly Serializer $serializer
private readonly array $postHookActions
) {
parent::__construct();
}
Expand All @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Dto/Aggregator/AggregateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public function getAccounts(): array;
public function setAccounts(array $accounts): void;

/**
* @return array<mixed>
* @return mixed
* @throws \Exception
*/
public function getData(): array;
public function getData(): mixed;

/**
* @param array<mixed> $data
* @param mixed $data
*/
public function setData(array $data): void;
public function setData(mixed $data): void;

/**
* @throws \Exception
Expand Down
10 changes: 5 additions & 5 deletions src/Dto/Aggregator/AggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class AggregateRoot implements AggregateInterface
/** @var array<mixed> */
private array $accounts = [];

/** @var array<mixed> */
private array $data = [];
/** @var mixed */
private mixed $data = null;

private ?string $session;

Expand All @@ -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!');
}

Expand Down

0 comments on commit 39d6fa4

Please sign in to comment.