src/Controller/Ads/Api/IndexAdsController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Ads\Api;
  3. use App\Controller\Ads\BaseAdsController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. /**
  9.  * @Route("/api", name="api_ads")
  10.  */
  11. class IndexAdsController extends BaseAdsController
  12. {
  13.     /**
  14.      * @Route("/ads", name="index")
  15.      * @return JsonResponse
  16.      */
  17.     public function __invoke(Request $request): Response
  18.     {
  19.         $response = [];
  20.         if(!$request->query->get('token')) {
  21.             return $this->json([
  22.                 'code' => 400,
  23.                 'status' => 'error',
  24.                 'message' => 'Forbidden'
  25.             ]);
  26.         }
  27.         $site $this->siteRepository->getOneToken($request->query->get('token'));
  28.         if($site == null) {
  29.             return $this->json([
  30.                 'code' => 400,
  31.                 'status' => 'error',
  32.                 'message' => 'Forbidden'
  33.             ]);
  34.         }
  35.         $items $this->adsRepository->listBy([
  36.             'site' => $site->getId(),
  37.             'order_by' => 'priority',
  38.             'type' => $request->query->get('type'),
  39.             'disabled' => true
  40.         ]);
  41.         $response['count'] = $this->adsRepository->getCount();
  42.         foreach ($items as $item) {
  43.             $response['body'][$item->getPosition()][] = $this->adsService->getFields($item);
  44.         }
  45.         return $this->json($response);
  46.     }
  47. }