<?php
namespace App\Controller\Content;
use App\Dto\SearchOptions;
use App\Service\Client\Content\CategoryService;
use App\Service\Client\Content\VideoListingService;
use App\Service\Search\VideoSearchService;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/kategorien/videos")
*/
class CategoryVideoController extends AbstractController
{
protected CategoryService $service;
protected VideoListingService $videoService;
protected VideoSearchService $searchService;
public function __construct(CategoryService $service, VideoListingService $videoService, VideoSearchService $searchService)
{
$this->service = $service;
$this->videoService = $videoService;
$this->searchService = $searchService;
}
/**
* @Route("")
* @Template()
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
*/
public function index(): array
{
return [
'tree' => $this->service->getTree()
];
}
/**
* @Route("/{slug}")
* @Template()
* @param string $slug
* @return array
*/
public function group(string $slug): array
{
$group = $this->service->getGroupBySlug($slug);
if (!$group) {
throw $this->createNotFoundException();
}
return [
'group' => $group,
];
}
/**
* @Route("/{group}/{slug}/{page}", requirements={"page": "\d+"}, defaults={"page": 1})
* @Template()
* @param string $group
* @param string $slug
* @param int $page
* @param Request $request
* @return array
* @throws \Psr\Cache\InvalidArgumentException
* @throws \ReflectionException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function category(string $group, string $slug, int $page, Request $request, LoggerInterface $logger): array
{
$searchOptions = new SearchOptions();
$searchOptions
->setPage($page)
->setPerPage(30)
->applySorting($request->query);
try {
$videoSearchResults = $this->searchService->findByCategorySlug($group, $slug, $searchOptions);
} catch (\Exception $e) {
$logger->critical($e->getMessage() . PHP_EOL . $e->getTraceAsString());
$videoSearchResults = [];
}
$category = $this->service->getCategoryBySlug($slug);
$group = $this->service->getGroupBySlug($group);
if (!$category || !$group) {
throw $this->createNotFoundException();
}
return [
'tree' => $this->service->getTree(),
'category' => $category,
'group' => $group,
'videos' => $videoSearchResults,
'page' => $page,
];
}
}