ldc-boutique-cnc-sable/app/Services/ActionExecutorService.php
Jérémy Munsch 42bdae01cc
feat: implement pagination using Pagination value object
- Updated DataTableService to accept a Pagination object for pagination parameters.
- Refactored paginateQuery method to utilize pageSize and pageNumber from Pagination.

refactor: replace ActionExecutor with Executor in controller traits

- Updated ChangesModelOrder, TogglesModelStar, and TogglesModelStatus traits to use Executor interface instead of ActionExecutor.

feat: add PictureMetadata and Slug value objects

- Introduced PictureMetadata for managing image metadata with validation.
- Added Slug value object for slug generation and validation.

feat: update breadcrumbs to support RangeDto

- Modified breadcrumbs-bo.php to accept RangeDto in the edit route.

test: add tests for RangesController

- Added tests for accessing, creating, updating, and deleting ranges.
- Included tests for toggling range status.

test: add integration tests for range actions

- Created tests for CreateRange, DeleteRange, and UpdateRange actions.
- Validated input handling and database interactions.

test: add unit tests for range input classes

- Implemented tests for CreateRangeInput and UpdateRangeInput to validate input constraints.

test: add unit tests for value objects

- Added tests for PictureMetadata and Slug to ensure proper validation and instantiation.

test: add unit tests for RangeDto

- Created tests to verify RangeDto mapping from Range model.

test: add unit tests for RangePolicy

- Implemented tests for RangePolicy to validate permissions based on user roles.
2026-10-27 09:58:34 +01:00

118 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 Override;
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.');
}
}
}