hydra-sql/src/Flag/Controller/FlagController.php
Gauthier DUPONT 12523398f6
Some checks are pending
Cadoles/hydra-sql/pipeline/pr-develop Build started...
feat(altcha): add altcha validation layer to login
2025-04-10 16:01:22 +02:00

38 lines
1.3 KiB
PHP

<?php
namespace App\Flag\Controller;
use App\Flag\FlagAccessor;
use App\Flag\FlagEnum;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FlagController extends AbstractController
{
#[Route('/flag/{flagName}', name: 'flag_update', methods: ['PUT'])]
public function updateFlag(CacheItemPoolInterface $cache, Request $request, string $flagName): Response
{
try {
FlagEnum::from($flagName);
$flagValue = \json_decode($request->getContent(), true, flags: JSON_THROW_ON_ERROR)[FlagAccessor::FLAG_VALUE];
} catch (\ValueError $e) {
throw new \InvalidArgumentException('invalid flag name provided');
} catch (JsonException $e) {
throw new \InvalidArgumentException('invalid json format');
}
$flag = $cache->getItem($flagName);
$flag->set($flagValue);
$cache->save($flag);
return new JsonResponse(
[\sprintf('flag %s has been %s.', $flagName, $flagValue ? 'enabled' : 'disabled')]
);
}
}