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