2026-09-29 13:03:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
use App\Contracts\Action;
|
|
|
|
|
use App\Contracts\ActionInput;
|
2026-09-29 16:59:07 +02:00
|
|
|
use App\Contracts\Executor;
|
|
|
|
|
use App\Contracts\Query;
|
|
|
|
|
use App\Contracts\QueryInput;
|
2026-09-29 13:03:19 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2026-09-29 16:59:07 +02:00
|
|
|
use Override;
|
2026-09-29 13:03:19 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
final class ActionExecutorService implements Executor
|
2026-09-29 13:03:19 +02:00
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly ?LoggerInterface $logger = null,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
#[Override]
|
|
|
|
|
public function execute(Action | Query $executable, ActionInput | QueryInput $input): mixed
|
2026-09-29 13:03:19 +02:00
|
|
|
{
|
2026-09-29 16:59:07 +02:00
|
|
|
$executableClass = $executable::class;
|
|
|
|
|
$actionName = class_basename($executableClass);
|
|
|
|
|
$type = $executable instanceof Action ? 'Action' : 'Query';
|
2026-09-29 13:03:19 +02:00
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
$this->logger?->info("[{$type}] Executing {$actionName}", [
|
|
|
|
|
'action' => $executableClass,
|
2026-09-29 13:03:19 +02:00
|
|
|
'input' => $input::class,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
try {
|
2026-09-29 16:59:07 +02:00
|
|
|
if ($executable instanceof Action) {
|
|
|
|
|
$this->assertInputIsAction($input);
|
|
|
|
|
|
|
|
|
|
return $this->executeWithinTransaction(
|
|
|
|
|
$executable,
|
|
|
|
|
$input,
|
|
|
|
|
$actionName
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-09-29 13:03:19 +02:00
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
$this->assertInputIsQuery($input);
|
2026-09-29 13:03:19 +02:00
|
|
|
|
2026-09-29 16:59:07 +02:00
|
|
|
return $this->executeWithoutTransaction(
|
|
|
|
|
$executable,
|
|
|
|
|
$input,
|
|
|
|
|
$actionName
|
|
|
|
|
);
|
2026-09-29 13:03:19 +02:00
|
|
|
} catch (\Throwable $e) {
|
2026-09-29 16:59:07 +02:00
|
|
|
$this->logger?->error("[{$type}] Failed {$actionName}: {$e->getMessage()}", [
|
|
|
|
|
'action' => $executableClass,
|
2026-09-29 13:03:19 +02:00
|
|
|
'input' => $input::class,
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-29 16:59:07 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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.');
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-29 13:03:19 +02:00
|
|
|
}
|