src/Controller/Content/PublicCategoryController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Content;
  3. use App\Service\Content\CategoryGroupService;
  4. use App\Service\Content\CategoryService;
  5. use App\StructSerializer\Main\StructOptions;
  6. use App\StructSerializer\Main\StructSerializer;
  7. use Nelmio\ApiDocBundle\Annotation\Model;
  8. use Swagger\Annotations as SWG;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * Class PublicCategoryController.
  14.  *
  15.  * @Route("/api/public/content/category")
  16.  */
  17. class PublicCategoryController extends AbstractController
  18. {
  19.     protected CategoryService $service;
  20.     protected CategoryGroupService $groupService;
  21.     protected StructSerializer $structSerializer;
  22.     public function __construct(CategoryService $serviceCategoryGroupService $groupServiceStructSerializer $structSerializer)
  23.     {
  24.         $this->service $service;
  25.         $this->groupService $groupService;
  26.         $this->structSerializer $structSerializer;
  27.     }
  28.     /**
  29.      * @SWG\Get(tags={"Content"},
  30.      *
  31.      *    @SWG\Response(response=200, description="Category tree for content",
  32.      *
  33.      *     @SWG\Schema(ref=@Model(type=App\Entity\CategoryGroup::class, groups={"categorylist"})))
  34.      * )
  35.      *
  36.      * @Route("/tree", methods={"GET"})
  37.      */
  38.     public function tree(): JsonResponse
  39.     {
  40.         $paginator $this->groupService->getTree();
  41.         $options StructOptions::create();
  42.         return new JsonResponse([
  43.             'data' => $this->structSerializer->multipleToArray($options$paginator),
  44.             'total' => $paginator->count(),
  45.         ]);
  46.     }
  47.     /**
  48.      * @SWG\Get(tags={"Content"},
  49.      *
  50.      *    @SWG\Response(response=200, description="Categorygroup details by its slug",
  51.      *
  52.      *     @SWG\Schema(ref=@Model(type=App\Entity\CategoryGroup::class, groups={"categorylist"}))),
  53.      *
  54.      *     @SWG\Parameter(name="slug", in="path", type="string", required=true, description="Slug of the category group"),
  55.      * )
  56.      *
  57.      * @Route("/group/{slug}", methods={"GET"})
  58.      */
  59.     public function group(string $slug): JsonResponse
  60.     {
  61.         if (!$group $this->groupService->findCategoryGroupBySlug($slug)) {
  62.             throw $this->createNotFoundException('Group not found for given slug.');
  63.         }
  64.         $options StructOptions::create();
  65.         return new JsonResponse($this->structSerializer->toArray($options$group));
  66.     }
  67.     /**
  68.      * @SWG\Get(tags={"Content"},
  69.      *
  70.      *    @SWG\Response(response=200, description="Category by slug",
  71.      *
  72.      *     @SWG\Schema(ref=@Model(type=App\Entity\Category::class, groups={"categorylist", "categorydetail"}))),
  73.      *
  74.      *     @SWG\Parameter(name="slug", in="path", type="string", required=true, description="Slug of the category"),
  75.      * )
  76.      *
  77.      * @Route("/category/{slug}", methods={"GET"})
  78.      */
  79.     public function category(string $slug): JsonResponse
  80.     {
  81.         if (!$category $this->service->findBySlug($slug)) {
  82.             throw $this->createNotFoundException('Category found for given slug.');
  83.         }
  84.         $options StructOptions::create();
  85.         return new JsonResponse($this->structSerializer->toArray($options$category));
  86.     }
  87. }