ogcore/src/Controller/OgDhcp/Subnet/DeleteClientLeaseAction.php

51 lines
1.8 KiB
PHP

<?php
namespace App\Controller\OgDhcp\Subnet;
use App\Controller\OgDhcp\AbstractOgDhcpController;
use App\Entity\Client;
use App\Entity\Subnet;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\AsController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
#[AsController]
class DeleteClientLeaseAction extends AbstractOgDhcpController
{
/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function __invoke(Subnet $data, string $clientUuid): JsonResponse
{
$client = $this->entityManager->getRepository(Client::class)->findOneBy(['uuid' => $clientUuid]);
if (!$client) {
throw new BadRequestHttpException('Client not found');
}
if (!$client->getIp()) {
throw new BadRequestHttpException('Client has no IP address assigned');
}
$dhcpServer = $data->getDhcpServer();
if (!$dhcpServer) {
throw new BadRequestHttpException('Subnet has no DHCP server associated');
}
$port = $dhcpServer->getApiPort() ?? 8081;
$url = sprintf('http://%s:%d/ogdhcp/v1/leases/%s', $dhcpServer->getIp(), $port, $client->getIp());
$content = $this->createRequest('DELETE', $url);
return new JsonResponse(data: $content, status: Response::HTTP_OK);
}
}