<?php
namespace MLDev\CatalogBundle\Controller;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Generator;
use MLDev\BaseBundle\Controller\FrontController;
use MLDev\BaseBundle\Service\PageManager;
use MLDev\CatalogBundle\Entity\Product;
use MLDev\CatalogBundle\Entity\ProductItem;
use MLDev\SeoSiteBundle\Entity\SeoSite;
use MLDev\SeoSiteBundle\Entity\SeoSiteInfo;
use MLDev\SeoSiteBundle\Service\SiteManager;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class ProductController
* @package MLDev\CatalogBundle\Controller
*/
class ProductController extends FrontController
{
/**
* @var array
*/
private $bundles = [];
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @param EntityManagerInterface $entityManager
* @param array $bundles
*/
public function __construct(EntityManagerInterface $entityManager, array $bundles)
{
$this->bundles = $bundles;
$this->entityManager = $entityManager;
}
/**
* @Route("/catalog/product-{id}.html", name="mldev-front-product-show", requirements={"id":"\d+"})
*/
public function showProductItem(ProductItem $entity, PageManager $pageManager, SiteManager $siteManager): Response
{
$product = $entity->getProduct();
$pages = $product->getPages();
if (!$pages->count()) {
throw $this->createNotFoundException('The page does not exist');
}
$virtualChild = $pageManager->createVirtualChildEntity(
$pages->first(), $entity->getName(), $product->getAlias(),
$this->generateUrl('mldev-front-product-show', ['id' => $entity->getId()])
);
$entity = $this->getMutationSeoInfoByProduct($entity, $siteManager);
$virtualChild->setSeoInfo(
$entity->getSeoInfo()
);
return $this->renderCustomTemplate($virtualChild, '@MLDevCatalog/templates/default.html.twig', [
'product' => $product,
'product_item' => $entity,
]);
}
private function getMutationSeoInfoByProduct(ProductItem $entity, SiteManager $siteManager): ProductItem
{
if (array_key_exists('MLDevSeoSiteBundle', $this->bundles)) {
if ($entity->getSeoInfo()->count() > 0) {
$seoInfoCollection = $entity->getSeoInfo();
$availableSites = [];
foreach ($seoInfoCollection->getIterator() as &$seoInfo) {
if ($seoInfo->getSeoSite()) {
$availableSites[$seoInfo->getSeoSite()->getId()] = $seoInfo->getSeoSite()->getId();
}
}
foreach ($siteManager->getAvailableSites() as $id => $site) {
if (!isset($availableSites[$id])) {
$site = $this->entityManager->getRepository(SeoSite::class)->find($id);
$seoSiteInfo = new SeoSiteInfo();
$seoSiteInfo->setSeoSite($site);
$seoInfoCollection->add($seoSiteInfo);
}
}
} else {
$seoSiteInfo = new SeoSiteInfo();
$seoSiteInfo->setSeoSite($siteManager->getCurrentSeoSite());
$seoInfoCollection = new ArrayCollection([$seoSiteInfo]);
}
} else {
$seoInfoCollection = $entity->getSeoInfo();
}
foreach ($seoInfoCollection->getIterator() as &$seoInfo) {
if (!$seoInfo->getDescription()) {
$description = sprintf('%s',
$entity->getName());
if ($seoInfo->getSeoSite()) {
$description .= sprintf(', %s', $seoInfo->getSeoSite()->getName());
}
$seoInfo->setDescription(
$this->escape($description)
);
}
if (!$seoInfo->getTitle()) {
$title = $entity->getName();
if ($seoInfo->getSeoSite()) {
$title .= sprintf(', %s', $seoInfo->getSeoSite()->getName());
}
$seoInfo->setTitle(
$this->escape($title)
);
}
}
$entity->setSeoInfo($seoInfoCollection);
return $entity;
}
private function escape(string $string)
{
$twig = $this->container->get('twig');
$escapeCallable = $twig->getFilter('escape')->getCallable();
return $twig->getRuntime($escapeCallable[0])->{$escapeCallable[1]}($string);
}
}