48 lines
1.4 KiB
PHP
48 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\ApiResource\DeployResult;
|
|
use App\Repository\EnvironmentRepository;
|
|
use App\Service\DeployService;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
final readonly class DeployProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private EnvironmentRepository $environmentRepository,
|
|
private DeployService $deployService,
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): DeployResult
|
|
{
|
|
$id = $uriVariables['id'] ?? null;
|
|
$environment = $id ? $this->environmentRepository->find($id) : null;
|
|
|
|
if (null === $environment) {
|
|
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
|
}
|
|
|
|
$requestData = $context['request']?->toArray() ?? [];
|
|
$tag = $requestData['tag'] ?? null;
|
|
|
|
if (null === $tag || '' === $tag) {
|
|
throw new BadRequestHttpException('The "tag" field is required.');
|
|
}
|
|
|
|
$result = $this->deployService->deploy($environment, $tag);
|
|
|
|
$dto = new DeployResult();
|
|
$dto->success = $result['success'];
|
|
$dto->output = $result['output'];
|
|
$dto->tag = $tag;
|
|
|
|
return $dto;
|
|
}
|
|
}
|