<?php
namespace App\Controller\Content;
use App\Service\Content\CategoryGroupService;
use App\Service\Content\CategoryService;
use App\StructSerializer\Main\StructOptions;
use App\StructSerializer\Main\StructSerializer;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class PublicCategoryController.
*
* @Route("/api/public/content/category")
*/
class PublicCategoryController extends AbstractController
{
protected CategoryService $service;
protected CategoryGroupService $groupService;
protected StructSerializer $structSerializer;
public function __construct(CategoryService $service, CategoryGroupService $groupService, StructSerializer $structSerializer)
{
$this->service = $service;
$this->groupService = $groupService;
$this->structSerializer = $structSerializer;
}
/**
* @SWG\Get(tags={"Content"},
*
* @SWG\Response(response=200, description="Category tree for content",
*
* @SWG\Schema(ref=@Model(type=App\Entity\CategoryGroup::class, groups={"categorylist"})))
* )
*
* @Route("/tree", methods={"GET"})
*/
public function tree(): JsonResponse
{
$paginator = $this->groupService->getTree();
$options = StructOptions::create();
return new JsonResponse([
'data' => $this->structSerializer->multipleToArray($options, $paginator),
'total' => $paginator->count(),
]);
}
/**
* @SWG\Get(tags={"Content"},
*
* @SWG\Response(response=200, description="Categorygroup details by its slug",
*
* @SWG\Schema(ref=@Model(type=App\Entity\CategoryGroup::class, groups={"categorylist"}))),
*
* @SWG\Parameter(name="slug", in="path", type="string", required=true, description="Slug of the category group"),
* )
*
* @Route("/group/{slug}", methods={"GET"})
*/
public function group(string $slug): JsonResponse
{
if (!$group = $this->groupService->findCategoryGroupBySlug($slug)) {
throw $this->createNotFoundException('Group not found for given slug.');
}
$options = StructOptions::create();
return new JsonResponse($this->structSerializer->toArray($options, $group));
}
/**
* @SWG\Get(tags={"Content"},
*
* @SWG\Response(response=200, description="Category by slug",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Category::class, groups={"categorylist", "categorydetail"}))),
*
* @SWG\Parameter(name="slug", in="path", type="string", required=true, description="Slug of the category"),
* )
*
* @Route("/category/{slug}", methods={"GET"})
*/
public function category(string $slug): JsonResponse
{
if (!$category = $this->service->findBySlug($slug)) {
throw $this->createNotFoundException('Category found for given slug.');
}
$options = StructOptions::create();
return new JsonResponse($this->structSerializer->toArray($options, $category));
}
}