ldc-ovoteam/app/Services/ActionExecutorService.php
Jérémy Munsch 57faeada75
feat(dto): add CategoryProductsDto, ProductDto, and ProductSummaryDto
Add new DTOs for handling product and category data structures.
These DTOs facilitate data transfer and serialization for products and categories.

refactor(controller): update ProductController to use executor pattern

Refactor ProductController to utilize an executor for handling product creation,
updating, and deletion, improving code organization and maintainability.

refactor(controller): update RecipeController to use executor pattern

Refactor RecipeController to utilize an executor for handling recipe creation,
improving code organization and maintainability.

chore: remove MediaController

Remove MediaController as it is no longer needed in the application.

feat(request): enhance StoreProductRequest and UpdateProductRequest

Enhance StoreProductRequest and UpdateProductRequest to include methods for
transforming request data into input objects for actions.

chore: add ActionExecutorService for executing actions and queries

Introduce ActionExecutorService to handle the execution of actions and queries
with logging and transaction management.

chore(deps): update composer dependencies

Update composer dependencies to include webmozart/assert for improved type assertions.
2026-11-05 11:37:20 +01:00

117 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Contracts\Executor;
use App\Contracts\Query;
use App\Contracts\QueryInput;
use Illuminate\Support\Facades\DB;
use Psr\Log\LoggerInterface;
final class ActionExecutorService implements Executor
{
public function __construct(
private readonly ?LoggerInterface $logger = null,
) {
}
#[\Override]
public function execute(Action | Query $executable, ActionInput | QueryInput $input): mixed
{
$executableClass = $executable::class;
$actionName = class_basename($executableClass);
$type = $executable instanceof Action ? 'Action' : 'Query';
$this->logger?->info("[{$type}] Executing {$actionName}", [
'action' => $executableClass,
'input' => $input::class,
]);
try {
if ($executable instanceof Action) {
$this->assertInputIsAction($input);
return $this->executeWithinTransaction(
$executable,
$input,
$actionName
);
}
$this->assertInputIsQuery($input);
return $this->executeWithoutTransaction(
$executable,
$input,
$actionName
);
} catch (\Throwable $e) {
$this->logger?->error("[{$type}] Failed {$actionName}: {$e->getMessage()}", [
'action' => $executableClass,
'input' => $input::class,
'message' => $e->getMessage(),
'exception' => $e,
]);
throw $e;
}
}
/**
* @template TQuery of Query
* @template TQueryInput of QueryInput
*
* @param TQuery $executable
* @param TQueryInput $input
*/
private function executeWithoutTransaction(Query $executable, QueryInput $input, string $actionName): mixed
{
$result = $executable($input);
$this->logger?->info("[Query] Succeeded {$actionName}");
return $result;
}
/**
* @template TAction of Action
* @template TActionInput of ActionInput
*
* @param TAction $executable
* @param TActionInput $input
*/
private function executeWithinTransaction(Action $executable, ActionInput $input, string $actionName): mixed
{
return DB::transaction(function () use ($executable, $input, $actionName): mixed {
$result = $executable($input);
$this->logger?->info("[Action] Succeeded {$actionName}");
return $result;
});
}
/**
* @psalm-assert QueryInput $input
*/
private function assertInputIsQuery(mixed $input): void
{
if (! $input instanceof QueryInput) {
throw new \InvalidArgumentException('Input must be an instance of QueryInput.');
}
}
/**
* @psalm-assert ActionInput $input
*/
private function assertInputIsAction(mixed $input): void
{
if (! $input instanceof ActionInput) {
throw new \InvalidArgumentException('Input must be an instance of ActionInput.');
}
}
}