ldc-ovoteam/app/Services/ActionExecutorService.php

118 lines
3.2 KiB
PHP
Raw Normal View History

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