<?php
namespace App\Controller\Content;
use App\Entity\Category;
use App\Entity\Content;
use App\Entity\ContentVideo;
use App\Entity\Member;
use App\Service\Content\ContentService;
use App\Service\Content\VideoService;
use App\StructSerializer\Main\StructOptions;
use App\StructSerializer\Main\StructSerializer;
use Doctrine\ORM\NonUniqueResultException;
use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/public/content")
*/
class PublicVideoController extends AbstractController
{
protected VideoService $service;
protected ContentService $contentService;
protected StructSerializer $serializer2;
protected StructOptions $listStructOptions;
public function __construct(
VideoService $service,
ContentService $contentService,
StructSerializer $serializer2,
) {
$this->service = $service;
$this->contentService = $contentService;
$this->serializer2 = $serializer2;
$this->listStructOptions = StructOptions::create();
$this->listStructOptions->skipProperty(Content::class, 'categories');
$this->listStructOptions->skipProperty(Content::class, 'comments');
$this->listStructOptions->skipProperty(Content::class, 'actors');
$this->listStructOptions->skipProperty(Content::class, 'imageset');
$this->listStructOptions->skipProperty(Member::class, 'main_photo');
$this->listStructOptions->skipProperty(Member::class, 'member_online');
}
/**
* @SWG\Get(tags={"Content", "Video"},
*
* @SWG\Response(response=200, description="Paginated list of the most recently published videos, active only",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist"}))),
*
* @SWG\Parameter(name="page", in="path", type="integer", required=true, description="Requested page for pagination"),
* @SWG\Parameter(name="limit", in="query", type="integer", required=false, default=20, description="Number of items per page for pagination")
* )
*
* @Route("/videos/{page}", methods={"GET"})
*/
public function index(Request $request, int $page): JsonResponse
{
$limit = $request->query->getInt('limit', 20);
$paginator = $this->service->getRepository()->getFreshVideos($page, $limit);
return new JsonResponse([
'data' => $this->serializer2->multipleToArray($this->listStructOptions,
$paginator->getIterator()->getArrayCopy()),
'total' => $paginator->count(),
]);
}
/**
* @SWG\Get(tags={"Content", "Video"},
*
* @SWG\Response(response=200, description="Details of the requested video ID",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist", "contentdetail", "previewphotolist", "memberonlineindex"}))),
*
* @SWG\Parameter(name="video", in="path", type="integer", required=true, description="Video ID in question"),
* )
*
* @Route("/video/{video}", methods={"GET"}, requirements={"video": "\d+"})
*/
public function detail(ContentVideo $video): JsonResponse
{
$options = StructOptions::create();
$options->skipProperty(Content::class, 'imageset');
$options->skipProperty(Member::class, 'member_online');
return new JsonResponse(
$this->serializer2->toArray($options, $video),
);
}
/**
* @SWG\Get(tags={"Content", "Video"},
*
* @SWG\Response(response=200, description="Find a video by slug",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist", "contentdetail", "actorlist", "previewphotolist", "memberonlineindex", "membermainphoto"}))),
*
* @SWG\Parameter(name="slug", in="path", type="string", required=true, description="Unique slug url"),
* @SWG\Parameter(name="ignoreContentStatus", in="query", type="boolean", required=false, description="Should content status ignored?")
* )
*
* @Route("/video/slug/{slug}", methods={"GET"}, priority=1)
*
* @throws NonUniqueResultException
*/
public function slug(Request $request, string $slug): JsonResponse
{
$ignoreContentStatus = (bool) $request->query->get('ignoreContentStatus') || false;
if (!$video = $this->service->findBySlug($slug, $ignoreContentStatus)) {
throw $this->createNotFoundException('Video not found by slug.');
}
$options = StructOptions::create();
$options->skipProperty(Content::class, 'imageset');
return new JsonResponse($this->serializer2->toArray($options, $video));
}
/**
* @SWG\Get(tags={"Content", "Video"},
*
* @SWG\Response(response=200, description="Paginated list of the best rated videos of the last 7 days",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist", "previewphotolist"}))),
*
* @SWG\Parameter(name="page", in="path", type="integer", description="Requested page for pagination"),
* @SWG\Parameter(name="limit", in="query", type="integer", description="Number of items per page for pagination")
* )
*
* @Route("/videos/bestoftheweek/{page}", methods={"GET"}, requirements={"page": "\d+"})
*
* @return JsonResponse
*/
public function bestoftheweek(Request $request, int $page)
{
$limit = $request->query->getInt('limit', 20);
$paginator = $this->service->getRepository()->getBestVideosOfTheWeek($page, $limit);
return new JsonResponse([
'data' => $this->serializer2->multipleToArray($this->listStructOptions,
$paginator->getIterator()->getArrayCopy()),
'total' => $paginator->count(),
]);
}
/**
* @SWG\Get(
* tags={"Content", "Video"},
*
* @SWG\Response(
* response=200,
* description="Paginated list of the best rated videos of the last 30 days",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist", "previewphotolist"}))),
*
* @SWG\Parameter(name="page", in="path", type="integer", required=true, description="Requested page for pagination"),
* @SWG\Parameter(name="limit", in="query", type="integer", required=false, default=20, description="Number of items per page for pagination")
* )
*
* @Route("/videos/bestofthemonth/{page}", methods={"GET"}, requirements={"page": "\d+"})
*
* @return JsonResponse
*/
public function bestofthemonth(Request $request, int $page)
{
$limit = $request->query->getInt('limit', 20);
$paginator = $this->service->getRepository()->getBestVideosOfTheMonth($page, $limit);
return new JsonResponse([
'data' => $this->serializer2->multipleToArray($this->listStructOptions,
$paginator->getIterator()->getArrayCopy()),
'total' => $paginator->count(),
]);
}
/**
* @SWG\Get(tags={"Content", "Video"},
*
* @SWG\Response(response=200, description="Paginated list of videos in a given category",
*
* @SWG\Schema(ref=@Model(type=App\Entity\Content::class, groups={"contentlist", "previewphotolist"}))),
*
* @SWG\Parameter(name="category", in="path", type="integer", required=true, description="Category ID in question"),
* @SWG\Parameter(name="page", in="path", type="integer", required=true, description="Requested page for pagination"),
* @SWG\Parameter(name="limit", in="query", type="integer", required=false, default=18, description="Number of items per page for pagination")
* )
*
* @Route("/videos/category/{category}/{page}", methods={"GET"}, requirements={"category": "\d+", "page": "\d+"})
*
* @return JsonResponse
*/
public function category(Request $request, Category $category, int $page)
{
$limit = $request->query->getInt('limit', 18);
$paginator = $this->service->getActiveVideosByCategory($category, $page, $limit);
return new JsonResponse([
'data' => $this->serializer2->multipleToArray($this->listStructOptions,
$paginator->getIterator()->getArrayCopy()),
'total' => $paginator->count(),
]);
}
}