*/ namespace Lc\CaracoleBundle\Resolver; use Doctrine\ORM\EntityManagerInterface; use Lc\CaracoleBundle\Model\Section\SectionInterface; use Lc\CaracoleBundle\Repository\Section\SectionRepository; use Lc\CaracoleBundle\Repository\Section\SectionStore; use Lc\SovBundle\Resolver\UrlResolver; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Security; class SectionResolver { protected bool $isCachedSection = false; protected ?SectionInterface $section = null; // cache front protected ?SectionInterface $cacheSectionCurrent = null; protected ?SectionInterface $cacheSectionVisited = null; protected ?SectionInterface $cacheSectionDefault = null; protected EntityManagerInterface $entityManager; protected Security $security; protected MerchantResolver $merchantResolver; protected SectionStore $sectionStore; protected RequestStack $requestStack; protected UrlResolver $urlResolver; public function __construct( EntityManagerInterface $entityManager, Security $security, MerchantResolver $merchantResolver, SectionStore $sectionStore, RequestStack $requestStack, UrlResolver $urlResolver ) { $this->entityManager = $entityManager; $this->security = $security; $this->merchantResolver = $merchantResolver; $this->sectionStore = $sectionStore; $this->requestStack = $requestStack; $this->urlResolver = $urlResolver; } public function getCurrent($returnDefaultIfOutOfSections = false, $returnVisitedIfOutOfSection = false, $throwExceptionIfSectionNotFound = true) { $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all(); // admin if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') { if ($this->cacheSectionCurrent) { return $this->cacheSectionCurrent; } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) { return $this->cacheSectionDefault; } $sectionCurrent = null; $userMerchant = $this->merchantResolver->getUserMerchant(); if($userMerchant !== null) { $sectionCurrent = $userMerchant->getCurrentAdminSection(); } $sectionDefault = $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault(); if($sectionCurrent) { $this->cacheSectionCurrent = $sectionCurrent; return $sectionCurrent; } elseif($returnDefaultIfOutOfSections && $sectionDefault) { $this->cacheSectionDefault = $sectionDefault; return $sectionDefault; } return null; } // front else { if ($this->cacheSectionCurrent) { return $this->cacheSectionCurrent; } elseif ($returnVisitedIfOutOfSection && $this->cacheSectionVisited) { return $this->cacheSectionVisited; } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) { return $this->cacheSectionDefault; } $userCurrent = $this->security->getUser(); $merchantCurrent = $this->merchantResolver->getCurrent(); $sectionStore = $this->sectionStore->setMerchant($merchantCurrent); $sectionCurrent = null; $sectionDefault = $sectionStore->getOneDefault(); $currentVisitedSection = null; if ($userCurrent && $userCurrent->getCurrentVisitedSection()) { $currentVisitedSection = $userCurrent->getCurrentVisitedSection(); } if (isset($requestAttributesArray['section'])) { $sectionCurrent = $sectionStore ->setMerchant($merchantCurrent) ->getOneBySlug($requestAttributesArray['section']); if($sectionCurrent === null && $throwExceptionIfSectionNotFound){ throw new NotFoundHttpException('Aucun espace n\'a été trouvé'); } } /* * /!\ Cache désactivé car génération d'une erreur du type : * "A new entity was found through the relationship 'App\Entity\Order\OrderShop#section' that was not configured to cascade persist operations for entity: Marché" * * Arrive sur les pages générées via le cache HTTP (CacheController.php) */ if ($sectionCurrent) { //$this->cacheSectionCurrent = $sectionCurrent; return $sectionCurrent; } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) { //$this->cacheSectionVisited = $currentVisitedSection; return $currentVisitedSection; } elseif ($returnDefaultIfOutOfSections && $sectionDefault) { //$this->cacheSectionDefault = $sectionDefault; return $sectionDefault; } return null; } } public function isOutOfSection(): bool { return $this->getCurrent(false, false, false) == null; } public function getDefault(): SectionInterface { return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault(); } }