feat(DetailsController): Crée un contrôleur pour récupérer les détails d'un site web EsyWeb.

This commit is contained in:
Serreau Jovann
2025-11-12 16:47:04 +01:00
parent ed586aed3c
commit f008903b8c

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Controller\Api\Private\EsyWeb;
use App\Entity\CustomerWalletHistory;
use App\Entity\EsyWeb\WebsiteDns;
use App\Entity\EsyWeb\WebsiteKey;
use App\Repository\EsyWeb\WebsiteDnsRepository;
use App\Repository\EsyWeb\WebsiteKeyRepository;
use App\Service\Stancer\Client;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
class DetailsController extends AbstractController
{
// Define a constant for the API key type
private const API_KEY_TYPE = 'api_key';
#[Route('/api/private/esyweb/details', name: 'api_private_esyweb_details', methods: ['GET'])]
public function wallet(
Request $request,
WebsiteDnsRepository $websiteDnsRepository,
WebsiteKeyRepository $websiteKeyRepository
): JsonResponse {
// Helper function to return a standardized 'unkown' error response
$errorResponse = function (): JsonResponse {
return $this->json([], Response::HTTP_BAD_REQUEST);
};
// 1. Get and validate required headers
$dns = $request->headers->get('EsyWebDns');
$apiKey = $request->headers->get('EsyWebApiKey');
if (!$dns || !$apiKey) {
// Note: The ApiSubscriber should ideally handle the *missing* headers.
// This handles the case where the headers might be present but empty strings.
return $errorResponse();
}
// 2. Find WebsiteDns
/** @var WebsiteDns|null $websiteDns */
$websiteDns = $websiteDnsRepository->findOneBy(['dns' => $dns]);
if (!$websiteDns) {
return $errorResponse();
}
// 3. Find WebsiteKey (checking against the constant for type)
/** @var WebsiteKey|null $websiteApiKey */
$websiteApiKey = $websiteKeyRepository->findOneBy([
'apiKey' => $apiKey,
'type' => self::API_KEY_TYPE
]);
if (!$websiteApiKey) {
return $errorResponse();
}
// 4. Cross-reference the Website objects
$websiteFromDns = $websiteDns->getWebsite();
$websiteFromKey = $websiteApiKey->getWebsitre();
if (!$websiteFromDns || !$websiteFromKey || $websiteFromDns->getId() !== $websiteFromKey->getId()) {
// This check ensures both the DNS and API Key belong to the same website
return $errorResponse();
}
$dnsList = [];
foreach ($websiteFromKey->getWebsiteDns() as $websiteDns) {
if($websiteDns->getType() == "fqdn") {
$dnsList[] = "www.".$websiteDns->getDns();
if($websiteDns->getDns() != $websiteFromDns->getMainDns())
$dnsList[] = $websiteDns->getDns();
} else {
if($websiteDns->getDns() != $websiteFromDns->getMainDns())
$dnsList[] = $websiteDns->getDns();
}
}
// 5. Success response
return $this->json([
'id' => $websiteFromDns->getId(),
'title' => $websiteFromDns->getTitle(),
'state' => $websiteFromDns->getState(),
'uuid' => $websiteFromDns->getUuid(),
'offert' => $websiteFromDns->getOffert(),
'type' => $websiteFromDns->getType(),
'mainDns' => $websiteFromDns->getMainDns(),
'dns' => implode(',', $dnsList),
]);
}
}