src/Controller/Main/ApiController.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Main;
  3. use App\Enum\ApiCodeEnum;
  4. use App\Enum\FileExtensionEnum;
  5. use App\Services\Main\SecurityService;
  6. use App\Services\Util\ConfigService;
  7. use Exception;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpClient\CurlHttpClient;
  10. use Symfony\Component\HttpClient\HttpClient;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. /**
  15.  * Class ApiController.
  16.  *
  17.  * @Route("api")
  18.  */
  19. class ApiController extends AbstractController
  20. {
  21.     /**
  22.      * @var ConfigService
  23.      */
  24.     private ConfigService $configService;
  25.     /**
  26.      * @var SecurityService
  27.      */
  28.     private SecurityService $securityService;
  29.     private string $apiUrl;
  30.     /**
  31.      * @var CurlHttpClient
  32.      */
  33.     private CurlHttpClient $client;
  34.     public function __construct(ConfigService $configServiceSecurityService $securityService)
  35.     {
  36.         $this->configService $configService;
  37.         $this->securityService $securityService;
  38.         $this->apiUrl $this->configService->getApiUrl();
  39.         $this->client HttpClient::create();
  40.     }
  41.     /**
  42.      * @Route("/{route}", name="api.main", requirements={"route" = ".+"})
  43.      */
  44.     public function index(Request $requeststring $route)
  45.     {
  46.         if ($token $this->securityService->getToken()) {
  47.             $options['auth_bearer'] = $token;
  48.         }
  49.         $options['auth_bearer'] = str_replace('Bearer '''$request->headers->get('authorization'));
  50.         $options['json'] = json_decode($request->getContent(), true);
  51.         $options['headers']['isRemote'] = true;
  52.         $query http_build_query($request->query->all());
  53.         $apiRoute $this->apiUrl $route;
  54.         if (!empty($query)) {
  55.             $apiRoute .= "?{$query}";
  56.         }
  57.         $response $this->client->request($request->getMethod(), $apiRoute$options);
  58.         if (Response::HTTP_OK !== $response->getStatusCode()) {
  59.             return $this->json([
  60.                 'error' => true,
  61.                 'message' => ApiCodeEnum::INVALID_REQUEST,
  62.             ]);
  63.         }
  64.         $headers = [];
  65.         try {
  66.             $headers $response->getHeaders();
  67.         } catch (Exception $e) {
  68.             return $this->json([
  69.                 'error' => true,
  70.                 'message' => $e->getMessage(),
  71.             ]);
  72.         }
  73.         $contentType mb_strtolower($headers['content-type'][0]);
  74.         if ('application/json' === $contentType && !empty($request->getContent())) {
  75.             return $this->json($response->toArray(false));
  76.         }
  77.         //Content-Type can contain "png" or "image/png" if resize or format is given
  78.         $extension explode('/'$contentType)[1] ?? '';
  79.         if (in_array($contentTypearray_keys(FileExtensionEnum::LIST_EXTENSIONStrue))
  80.             || in_array($extensionarray_keys(FileExtensionEnum::LIST_EXTENSIONStrue))
  81.         ) {
  82.             $response = new Response($response->getContent());
  83.             $response->headers->set('Content-type'$contentType);
  84.             $response->headers->set('Content-Disposition'$headers['content-disposition'][0]);
  85.             return $response;
  86.         }
  87.         if (str_contains($contentType'text/html')) {
  88.             return new Response($response->getContent());
  89.         }
  90.         return $this->json([
  91.             'error' => true,
  92.             'message' => ApiCodeEnum::INVALID_REQUEST,
  93.         ]);
  94.     }
  95. }