- Added CreateTag, UpdateTag, and DeleteTag actions. - Introduced CreateTagInput and UpdateTagInput for input validation. - Implemented ActionExecutor for executing actions with logging. - Added tests for all new actions and input validation.
59 lines
1.6 KiB
PHP
59 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionExecutor;
|
|
use App\Contracts\ActionInput;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
final class ActionExecutorService implements ActionExecutor
|
|
{
|
|
public function __construct(
|
|
private readonly ?LoggerInterface $logger = null,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @template TInput of \App\Contracts\ActionInput
|
|
* @template TResult
|
|
*
|
|
* @param \App\Contracts\Action<TInput, TResult> $action
|
|
* @param TInput $input
|
|
* @return TResult
|
|
*/
|
|
#[\Override]
|
|
public function execute(Action $action, ActionInput $input): mixed
|
|
{
|
|
$actionClass = $action::class;
|
|
$actionName = class_basename($actionClass);
|
|
|
|
$this->logger?->info("[Action] Executing {$actionName}", [
|
|
'action' => $actionClass,
|
|
'input' => $input::class,
|
|
]);
|
|
|
|
try {
|
|
/** @var TResult */
|
|
return DB::transaction(function () use ($action, $input, $actionName): mixed {
|
|
$result = $action($input);
|
|
|
|
$this->logger?->info("[Action] Succeeded {$actionName}");
|
|
|
|
return $result;
|
|
});
|
|
} catch (\Throwable $e) {
|
|
$this->logger?->error("[Action] Failed {$actionName}: {$e->getMessage()}", [
|
|
'action' => $actionClass,
|
|
'input' => $input::class,
|
|
'message' => $e->getMessage(),
|
|
'exception' => $e,
|
|
]);
|
|
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|