bundles/mldev-catalog-bundle/src/Controller/ProductController.php line 141

Open in your IDE?
  1. <?php
  2. namespace MLDev\CatalogBundle\Controller;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Generator;
  6. use MLDev\BaseBundle\Controller\FrontController;
  7. use MLDev\BaseBundle\Service\PageManager;
  8. use MLDev\CatalogBundle\Entity\Product;
  9. use MLDev\CatalogBundle\Entity\ProductItem;
  10. use MLDev\SeoSiteBundle\Entity\SeoSite;
  11. use MLDev\SeoSiteBundle\Entity\SeoSiteInfo;
  12. use MLDev\SeoSiteBundle\Service\SiteManager;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. /**
  16.  * Class ProductController
  17.  * @package MLDev\CatalogBundle\Controller
  18.  */
  19. class ProductController extends FrontController
  20. {
  21.     /**
  22.      * @var array
  23.      */
  24.     private $bundles = [];
  25.     /**
  26.      * @var EntityManagerInterface
  27.      */
  28.     private $entityManager;
  29.     /**
  30.      * @param EntityManagerInterface $entityManager
  31.      * @param array $bundles
  32.      */
  33.     public function __construct(EntityManagerInterface $entityManager, array $bundles)
  34.     {
  35.         $this->bundles $bundles;
  36.         $this->entityManager $entityManager;
  37.     }
  38.     /**
  39.      * @Route("/catalog/product-{id}.html", name="mldev-front-product-show", requirements={"id":"\d+"})
  40.      */
  41.     public function showProductItem(ProductItem $entityPageManager $pageManagerSiteManager $siteManager): Response
  42.     {
  43.         $product $entity->getProduct();
  44.         $pages $product->getPages();
  45.         if (!$pages->count()) {
  46.             throw $this->createNotFoundException('The page does not exist');
  47.         }
  48.         $virtualChild $pageManager->createVirtualChildEntity(
  49.             $pages->first(), $entity->getName(), $product->getAlias(),
  50.             $this->generateUrl('mldev-front-product-show', ['id' => $entity->getId()])
  51.         );
  52.         $entity $this->getMutationSeoInfoByProduct($entity$siteManager);
  53.         $virtualChild->setSeoInfo(
  54.             $entity->getSeoInfo()
  55.         );
  56.         return $this->renderCustomTemplate($virtualChild'@MLDevCatalog/templates/default.html.twig', [
  57.             'product' => $product,
  58.             'product_item' => $entity,
  59.         ]);
  60.     }
  61.     private function getMutationSeoInfoByProduct(ProductItem $entitySiteManager $siteManager): ProductItem
  62.     {
  63.         if (array_key_exists('MLDevSeoSiteBundle'$this->bundles)) {
  64.             if ($entity->getSeoInfo()->count() > 0) {
  65.                 $seoInfoCollection $entity->getSeoInfo();
  66.                 $availableSites = [];
  67.                 foreach ($seoInfoCollection->getIterator() as &$seoInfo) {
  68.                     if ($seoInfo->getSeoSite()) {
  69.                         $availableSites[$seoInfo->getSeoSite()->getId()] = $seoInfo->getSeoSite()->getId();
  70.                     }
  71.                 }
  72.                 foreach ($siteManager->getAvailableSites() as $id => $site) {
  73.                     if (!isset($availableSites[$id])) {
  74.                         $site $this->entityManager->getRepository(SeoSite::class)->find($id);
  75.                         $seoSiteInfo = new SeoSiteInfo();
  76.                         $seoSiteInfo->setSeoSite($site);
  77.                         $seoInfoCollection->add($seoSiteInfo);
  78.                     }
  79.                 }
  80.             } else {
  81.                 $seoSiteInfo = new SeoSiteInfo();
  82.                 $seoSiteInfo->setSeoSite($siteManager->getCurrentSeoSite());
  83.                 $seoInfoCollection = new ArrayCollection([$seoSiteInfo]);
  84.             }
  85.         } else {
  86.             $seoInfoCollection $entity->getSeoInfo();
  87.         }
  88.         foreach ($seoInfoCollection->getIterator() as &$seoInfo) {
  89.             if (!$seoInfo->getDescription()) {
  90.                 $description sprintf('%s',
  91.                     $entity->getName());
  92.                 if ($seoInfo->getSeoSite()) {
  93.                     $description .= sprintf(', %s'$seoInfo->getSeoSite()->getName());
  94.                 }
  95.                 $seoInfo->setDescription(
  96.                     $this->escape($description)
  97.                 );
  98.             }
  99.             if (!$seoInfo->getTitle()) {
  100.                 $title $entity->getName();
  101.                 if ($seoInfo->getSeoSite()) {
  102.                     $title .= sprintf(', %s'$seoInfo->getSeoSite()->getName());
  103.                 }
  104.                 $seoInfo->setTitle(
  105.                     $this->escape($title)
  106.                 );
  107.             }
  108.         }
  109.         $entity->setSeoInfo($seoInfoCollection);
  110.         return $entity;
  111.     }
  112.     private function escape(string $string)
  113.     {
  114.         $twig $this->container->get('twig');
  115.         $escapeCallable $twig->getFilter('escape')->getCallable();
  116.         return $twig->getRuntime($escapeCallable[0])->{$escapeCallable[1]}($string);
  117.     }
  118. }