|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * @author La clic ! <contact@laclic.fr>
- */
-
- 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 MerchantResolver $merchantResolver;
- protected SectionStore $sectionStore;
- protected RequestStack $requestStack;
- protected UrlResolver $urlResolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- MerchantResolver $merchantResolver,
- SectionStore $sectionStore,
- RequestStack $requestStack,
- UrlResolver $urlResolver
- )
- {
- $this->entityManager = $entityManager;
- $this->merchantResolver = $merchantResolver;
- $this->sectionStore = $sectionStore;
- $this->requestStack = $requestStack;
- $this->urlResolver = $urlResolver;
- }
-
- public function getCurrent()
- {
- $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();
-
- // admin
- if (isset($requestAttributesArray['easyadmin_context'])) {
- if (!$this->isCachedSection) {
- $currentAdminSection = null;
- $userMerchant = $this->merchantResolver->getUserMerchant();
-
- if ($userMerchant !== null) {
- $currentAdminSection = $userMerchant->getCurrentAdminSection();
- }
-
- /*if ($currentAdminSection === null) {
- $currentAdminSection = $this->sectionStore
- ->setMerchant($userMerchant->getMerchant())
- ->getOneDefault();
-
- if ($currentAdminSection === null) {
- throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
- }
- }*/
-
- $this->isCachedSection = true;
- $this->section = $currentAdminSection;
-
- return $currentAdminSection;
- }else{
- return $this->section;
- }
- } // front
- else {
- if($this->section === null) {
- $merchantCurrent = $this->merchantResolver->getCurrent();
- $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
- $sectionCurrent = null;
- $sectionDefault = $sectionStore->getOneDefault();
-
- if (isset($requestAttributesArray['section'])) {
- $sectionCurrent = $sectionStore
- ->setMerchant($merchantCurrent)
- ->getOneBySlug($requestAttributesArray['section']);
- }
-
- $this->section = $sectionCurrent ?: $sectionDefault;
- }
-
- return $this->section;
- }
- }
-
- public function isOutOfSection(): bool
- {
- return $this->getCurrent() == null;
- }
-
- public function getDefault(): SectionInterface
- {
- return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
- }
-
-
- }
|