64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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;
|
|
|
|
final readonly class MaintenanceToggleProcessor implements ProcessorInterface
|
|
{
|
|
/**
|
|
* @param list<array{name: string, slug: string, maintenance_path: string}> $managedApplications
|
|
*/
|
|
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
|
|
{
|
|
$slug = $uriVariables['slug'] ?? '';
|
|
$appConfig = null;
|
|
|
|
foreach ($this->managedApplications as $app) {
|
|
if ($app['slug'] === $slug) {
|
|
$appConfig = $app;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (null === $appConfig) {
|
|
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
|
|
}
|
|
|
|
$maintenancePath = $appConfig['maintenance_path'];
|
|
|
|
if ($data->maintenance) {
|
|
$directory = dirname($maintenancePath);
|
|
|
|
if (!is_dir($directory)) {
|
|
mkdir($directory, 0755, true);
|
|
}
|
|
|
|
touch($maintenancePath);
|
|
} elseif (file_exists($maintenancePath)) {
|
|
unlink($maintenancePath);
|
|
}
|
|
|
|
$dto = new ManagedApplication();
|
|
$dto->slug = $appConfig['slug'];
|
|
$dto->name = $appConfig['name'];
|
|
$dto->maintenance = file_exists($maintenancePath);
|
|
|
|
return $dto;
|
|
}
|
|
}
|