Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
- MeProvider : guard null user avec AccessDeniedHttpException - MaintenanceToggleProcessor : vérification des opérations filesystem - User : restreindre Get/GetCollection aux ROLE_ADMIN - useAppVersion : corriger le path relatif '/version' Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
68 lines
2.1 KiB
PHP
68 lines
2.1 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)) {
|
|
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));
|
|
}
|
|
}
|
|
|
|
$dto = new ManagedApplication();
|
|
$dto->slug = $appConfig['slug'];
|
|
$dto->name = $appConfig['name'];
|
|
$dto->maintenance = file_exists($maintenancePath);
|
|
|
|
return $dto;
|
|
}
|
|
}
|