*/ 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\Security\Core\Security; class SectionResolver { protected bool $isCachedSection = false; protected ?SectionInterface $section = 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) { $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all(); // admin if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') { if (!$this->isCachedSection) { $currentAdminSection = null; $userMerchant = $this->merchantResolver->getUserMerchant(); if ($userMerchant !== null) { $currentAdminSection = $userMerchant->getCurrentAdminSection(); } $this->isCachedSection = true; $this->section = $currentAdminSection; return $currentAdminSection; } else { return $this->section; } } // front else { $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) { return $sectionCurrent; } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) { return $currentVisitedSection; } elseif ($returnDefaultIfOutOfSections && $sectionDefault) { return $sectionDefault; } return null; } } public function isOutOfSection(): bool { return $this->getCurrent() == null; } public function getDefault(): SectionInterface { return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault(); } }