src/Controller/Api/ArticleCategoryController.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\ArticleCategory;
  4. use App\Service\Rsa;
  5. use App\Service\SerializeService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. /**
  13.  * Class ArticleCategoryController
  14.  * @package App\Controller\Api
  15.  * @Route(path="/api/article/category", name="api_article_category_")
  16.  */
  17. class ArticleCategoryController extends AbstractController
  18. {
  19.     private EntityManagerInterface $em;
  20.     private SerializeService $serializeService;
  21.     private Rsa $rsa;
  22.     private TranslatorInterface $translator;
  23.     public function __construct(
  24.         EntityManagerInterface $em,
  25.         SerializeService $serializeService,
  26.         Rsa $rsa,
  27.         TranslatorInterface $translator
  28.     ) {
  29.         $this->em $em;
  30.         $this->serializeService $serializeService;
  31.         $this->rsa $rsa;
  32.         $this->translator $translator;
  33.     }
  34.     /**
  35.      * @param Request $request
  36.      * @return JsonResponse
  37.      * @Route("", name="find_all")
  38.      */
  39.     public function findAll(Request $request): JsonResponse
  40.     {
  41.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  42.             return new JsonResponse([
  43.                 'success' => false,
  44.                 'message' => $this->translator->trans('global.invalidToken'),
  45.             ]);
  46.         }
  47.         $data = [];
  48.         /** @var ArticleCategory[] $articleCategories */
  49.         $articleCategories $this->em->getRepository(ArticleCategory::class)->findBy([], ['name' => 'asc']);
  50.         foreach ($articleCategories as $articleCategory) {
  51.             $data[] = $this->serializeService->serializeArticleCategory($articleCategory);
  52.         }
  53.         return new JsonResponse([
  54.             'success' => true,
  55.             'articleCategories' => $data,
  56.         ]);
  57.     }
  58.     /**
  59.      * @param ArticleCategory $articleCategory
  60.      * @param Request $request
  61.      * @return JsonResponse
  62.      * @Route("/{articleCategory}", name="find", requirements={"articleCategory"="\d+"})
  63.      */
  64.     public function find(ArticleCategory $articleCategoryRequest $request): JsonResponse
  65.     {
  66.         if (!$this->rsa->isValidToken($request->request->get('apiKey'))) {
  67.             return new JsonResponse([
  68.                 'success' => false,
  69.                 'message' => $this->translator->trans('global.invalidToken'),
  70.             ]);
  71.         }
  72.         return new JsonResponse([
  73.             'success' => true,
  74.             'articleCategory' => $this->serializeService->serializeArticleCategory($articleCategory),
  75.         ]);
  76.     }
  77. }