bundles/mldev-publication-bundle/src/Controller/ItemController.php line 25

Open in your IDE?
  1. <?php
  2. namespace MLDev\PublicationBundle\Controller;
  3. use Doctrine\ORM\NoResultException;
  4. use MLDev\BaseBundle\Controller\FrontController;
  5. use MLDev\BaseBundle\Entity\Page;
  6. use MLDev\BaseBundle\Service\PageManager;
  7. use MLDev\PublicationBundle\Entity\Item;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. /**
  14.  * Class ItemController
  15.  * @package MLDev\PublicationBundle\Controller
  16.  */
  17. class ItemController extends FrontController
  18. {
  19.     /**
  20.      * @Route("/{uri}/{alias}-publication-{id}.html", name="mldev-front-publication-show", requirements={"uri":".*", "alias":".*", "id":"\d+"})
  21.      */
  22.     public function show(PageManager $pageManagerRequest $requestItem $itemstring $uristring $alias): Response
  23.     {
  24.         // validate request and redirect to validated url
  25.         if (($valid $this->validateRequest($item$request)) && $valid instanceof Response) {
  26.             return $valid;
  27.         }
  28.         try {
  29.             $page $this->getDoctrine()->getRepository(Page::class)->getPageByUri($uri);
  30.         } catch (NoResultException $exception) {
  31.             throw $this->createNotFoundException('Not found'$exception);
  32.         }
  33.         $url $this->getUrlByEntity($item);
  34.         $virtualChild $pageManager->createVirtualChildEntity(
  35.             $item->getCategory()->getPage(),
  36.             $item->getName(),
  37.             $item->getAlias(),
  38.             $url
  39.         );
  40.         $virtualChild->setSeoInfo(
  41.             $item->getSeoInfo()
  42.         );
  43.         $otherLastThreeQueryBuilder $this->getDoctrine()->getRepository(Item::class)->getOtherLastThreeQueryBuilder(
  44.             $item->getCategory()->getId()
  45.         );
  46.         $otherLastThreeQueryBuilder->andWhere(
  47.             $otherLastThreeQueryBuilder->expr()->neq('item.id'$item->getId())
  48.         );
  49.         return $this->renderCustomTemplate($virtualChild'@MLDevPublication/templates/default.html.twig', [
  50.             'item' => $item,
  51.             'other_news_list' => $otherLastThreeQueryBuilder->getQuery()->getResult()
  52.         ]);
  53.     }
  54.     /**
  55.      * @return boolean|Response
  56.      */
  57.     private function validateRequest(Item $entityRequest $request)
  58.     {
  59.         $url $this->getUrlByEntity($entity);
  60.         if ($request->getPathInfo() !== $url) {
  61.             return $this->redirect($url . ($request->getQueryString() ? '?' $request->getQueryString() : ''));
  62.         }
  63.         return true;
  64.     }
  65.     private function getUrlByEntity(Item $entity): string
  66.     {
  67.         return $this->generateUrl('mldev-front-publication-show', [
  68.             'uri' => str_replace('/'''$entity->getCategory()->getPage()->getUri()),
  69.             'alias' => $entity->getAlias(),
  70.             'id' => $entity->getId(),
  71.         ]);
  72.     }
  73. }