✨ feat(StatusController): Améliore la gestion des requêtes et des erreurs.
This commit is contained in:
@@ -4,48 +4,74 @@ namespace App\Controller\Api\Private\EsyWeb;
|
||||
|
||||
use App\Entity\EsyWeb\WebsiteDns;
|
||||
use App\Entity\EsyWeb\WebsiteKey;
|
||||
use App\Repository\ComputeRepository;
|
||||
use App\Repository\EsyWeb\WebsiteDnsRepository;
|
||||
use App\Repository\EsyWeb\WebsiteKeyRepository;
|
||||
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 StatusController extends AbstractController
|
||||
{
|
||||
#[Route('/api/private/esyweb/status', name: 'api_private_esyweb_status', methods: ['GET'])]
|
||||
public function status(Request $request,WebsiteDnsRepository $websiteDnsRepository,WebsiteKeyRepository $websiteKeyRepository)
|
||||
{
|
||||
$dns = $request->headers->get('EsyWebDns','');
|
||||
$apiKey = $request->headers->get('EsyWebApiKey','');
|
||||
if($dns == "")
|
||||
return $this->json([
|
||||
'status' => 'unkown'
|
||||
],Response::HTTP_BAD_REQUEST);
|
||||
// Define a constant for the API key type
|
||||
private const API_KEY_TYPE = 'api_key';
|
||||
|
||||
#[Route('/api/private/esyweb/status', name: 'api_private_esyweb_status', methods: ['GET'])]
|
||||
public function status(
|
||||
Request $request,
|
||||
WebsiteDnsRepository $websiteDnsRepository,
|
||||
WebsiteKeyRepository $websiteKeyRepository
|
||||
): JsonResponse {
|
||||
|
||||
// Helper function to return a standardized 'unkown' error response
|
||||
$errorResponse = function (): JsonResponse {
|
||||
return $this->json(['status' => 'unkown'], 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 instanceof WebsiteDns) {
|
||||
return $this->json([
|
||||
'status' => 'unkown'
|
||||
],Response::HTTP_BAD_REQUEST);
|
||||
|
||||
if (!$websiteDns) {
|
||||
return $errorResponse();
|
||||
}
|
||||
$websiteApiKey = $websiteKeyRepository->findOneBy(['apiKey' => $apiKey,'type'=>'api_key']);
|
||||
if(!$websiteApiKey instanceof WebsiteKey) {
|
||||
return $this->json([
|
||||
'status' => 'unkown'
|
||||
],Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
$website = $websiteDns->getWebsite();
|
||||
$websiteKey = $websiteApiKey->getWebsitre();
|
||||
if($website->getId() != $websiteKey->getId()) {
|
||||
return $this->json([
|
||||
'status' => 'unkown'
|
||||
],Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
return $this->json([
|
||||
'status' => $website->getState(),
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
// 5. Success response
|
||||
return $this->json([
|
||||
'status' => $websiteFromDns->getState(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user