53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\Environment;
|
|
use App\Service\AppPathResolver;
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private AppPathResolver $pathResolver,
|
|
) {}
|
|
|
|
/**
|
|
* @param Environment $data
|
|
*/
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
|
|
{
|
|
$relativePath = $data->getMaintenanceFilePath();
|
|
|
|
if (null === $relativePath) {
|
|
throw new BadRequestHttpException('Maintenance file path is not configured for this environment.');
|
|
}
|
|
|
|
$maintenancePath = $this->pathResolver->resolve($relativePath);
|
|
$requestData = $context['request']?->toArray() ?? [];
|
|
$enableMaintenance = $requestData['maintenance'] ?? false;
|
|
|
|
if ($enableMaintenance) {
|
|
$directory = dirname($maintenancePath);
|
|
|
|
if (!is_dir($directory) && !mkdir($directory, 0755, true)) {
|
|
throw new \RuntimeException(sprintf('Cannot create directory "%s".', $directory));
|
|
}
|
|
|
|
if (!touch($maintenancePath)) {
|
|
throw new \RuntimeException(sprintf('Cannot create maintenance file at "%s".', $maintenancePath));
|
|
}
|
|
} elseif (file_exists($maintenancePath)) {
|
|
if (!unlink($maintenancePath)) {
|
|
throw new \RuntimeException(sprintf('Cannot remove maintenance file at "%s".', $maintenancePath));
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
}
|