<?php
namespace App\Action\PressSite\Product;
use App\Action\PressSite\DomainAwareAction;
use App\Contract\PressSite\Language\Switcher\EntityActionInterface;
use App\Entity\Product;
use App\Entity\ProductTVSeries;
use App\Repository\ProductRepository;
use App\Service\Helper\PressSite\MultimediaHelper;
use App\Service\PressSite\DomainAwareManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;
/**
* Class SingleMovieAction.
*/
class SingleMovieAction extends DomainAwareAction implements EntityActionInterface
{
/**
* The instance of the product entity repository.
*
* @var \App\Repository\ProductRepository
*/
private $productRepository;
public function __construct(
Environment $twig,
DomainAwareManager $domainAwareManager,
ProductRepository $productRepository
) {
parent::__construct($twig, $domainAwareManager);
$this->productRepository = $productRepository;
}
/**
* Responsible for loading single product.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object
* @param int $id
* The ID of the product entity to load
*
* @return \Symfony\Component\HttpFoundation\Response
* The response object to return
*/
public function __invoke(Request $request, int $id): Response
{
$entity = $this->productRepository->findOneByDomainAndLanguageCode(
$id,
$this->getDomainManager()->getCurrentByHostnameAndLocale()->getId(),
$this->getDomainManager()->getCurrentByHostnameAndLocale()->getLanguageCode()
);
if (!$entity instanceof Product) {
throw new NotFoundHttpException('Product not found');
}
$request->attributes->set('_product_id', $id);
$data = $request->getSession()->get(MultimediaHelper::createStorageKey($request));
$itemsCount = (!empty($data) && !empty($data['count'])) ? $data['count'] : 0;
$isTvSeries = $entity instanceof ProductTVSeries;
return $this->render('press_site/actions/movies/single_movie.html.twig', [
'entity' => $entity,
'is_tv_series' => $isTvSeries,
'storage_items_count' => $itemsCount,
]);
}
}