refactor : rewrite MaintenanceToggleProcessor for Environment entity
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,42 +6,26 @@ namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\ApiResource\ManagedApplication;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use App\Entity\Environment;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
|
||||
final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
||||
* @param Environment $data
|
||||
*/
|
||||
public function __construct(
|
||||
#[Autowire('%app.managed_applications%')]
|
||||
private array $managedApplications,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ManagedApplication $data
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): ManagedApplication
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
|
||||
{
|
||||
$slug = $uriVariables['slug'] ?? '';
|
||||
$appConfig = null;
|
||||
$maintenancePath = $data->getMaintenanceFilePath();
|
||||
|
||||
foreach ($this->managedApplications as $app) {
|
||||
if ($app['slug'] === $slug) {
|
||||
$appConfig = $app;
|
||||
break;
|
||||
}
|
||||
if (null === $maintenancePath) {
|
||||
throw new BadRequestHttpException('Maintenance file path is not configured for this environment.');
|
||||
}
|
||||
|
||||
if (null === $appConfig) {
|
||||
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
||||
}
|
||||
$requestData = $context['request']?->toArray() ?? [];
|
||||
$enableMaintenance = $requestData['maintenance'] ?? false;
|
||||
|
||||
$maintenancePath = $appConfig['maintenance_path'];
|
||||
|
||||
if ($data->maintenance) {
|
||||
if ($enableMaintenance) {
|
||||
$directory = dirname($maintenancePath);
|
||||
|
||||
if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
|
||||
@@ -57,11 +41,6 @@ final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
||||
}
|
||||
}
|
||||
|
||||
$dto = new ManagedApplication();
|
||||
$dto->slug = $appConfig['slug'];
|
||||
$dto->name = $appConfig['name'];
|
||||
$dto->maintenance = file_exists($maintenancePath);
|
||||
|
||||
return $dto;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user