Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 lines
5.5KB

  1. <?php
  2. /**
  3. * @author La clic ! <contact@laclic.fr>
  4. */
  5. namespace Lc\CaracoleBundle\Resolver;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  9. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  10. use Lc\SovBundle\Resolver\UrlResolver;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Security\Core\Security;
  14. class SectionResolver
  15. {
  16. protected bool $isCachedSection = false;
  17. protected ?SectionInterface $section = null;
  18. // cache front
  19. protected ?SectionInterface $cacheSectionCurrent = null;
  20. protected ?SectionInterface $cacheSectionVisited = null;
  21. protected ?SectionInterface $cacheSectionDefault = null;
  22. protected EntityManagerInterface $entityManager;
  23. protected Security $security;
  24. protected MerchantResolver $merchantResolver;
  25. protected SectionStore $sectionStore;
  26. protected RequestStack $requestStack;
  27. protected UrlResolver $urlResolver;
  28. public function __construct(
  29. EntityManagerInterface $entityManager,
  30. Security $security,
  31. MerchantResolver $merchantResolver,
  32. SectionStore $sectionStore,
  33. RequestStack $requestStack,
  34. UrlResolver $urlResolver
  35. ) {
  36. $this->entityManager = $entityManager;
  37. $this->security = $security;
  38. $this->merchantResolver = $merchantResolver;
  39. $this->sectionStore = $sectionStore;
  40. $this->requestStack = $requestStack;
  41. $this->urlResolver = $urlResolver;
  42. }
  43. public function getCurrent($returnDefaultIfOutOfSections = false, $returnVisitedIfOutOfSection = false, $throwExceptionIfSectionNotFound = true)
  44. {
  45. $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();
  46. // admin
  47. if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') {
  48. if ($this->cacheSectionCurrent) {
  49. return $this->cacheSectionCurrent;
  50. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  51. return $this->cacheSectionDefault;
  52. }
  53. $sectionCurrent = null;
  54. $userMerchant = $this->merchantResolver->getUserMerchant();
  55. if($userMerchant !== null) {
  56. $sectionCurrent = $userMerchant->getCurrentAdminSection();
  57. }
  58. $sectionDefault = $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  59. if($sectionCurrent) {
  60. $this->cacheSectionCurrent = $sectionCurrent;
  61. return $sectionCurrent;
  62. } elseif($returnDefaultIfOutOfSections && $sectionDefault) {
  63. $this->cacheSectionDefault = $sectionDefault;
  64. return $sectionDefault;
  65. }
  66. return null;
  67. } // front
  68. else {
  69. if ($this->cacheSectionCurrent) {
  70. return $this->cacheSectionCurrent;
  71. } elseif ($returnVisitedIfOutOfSection && $this->cacheSectionVisited) {
  72. return $this->cacheSectionVisited;
  73. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  74. return $this->cacheSectionDefault;
  75. }
  76. $userCurrent = $this->security->getUser();
  77. $merchantCurrent = $this->merchantResolver->getCurrent();
  78. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  79. $sectionCurrent = null;
  80. $sectionDefault = $sectionStore->getOneDefault();
  81. $currentVisitedSection = null;
  82. if ($userCurrent && $userCurrent->getCurrentVisitedSection()) {
  83. $currentVisitedSection = $userCurrent->getCurrentVisitedSection();
  84. }
  85. if (isset($requestAttributesArray['section'])) {
  86. $sectionCurrent = $sectionStore
  87. ->setMerchant($merchantCurrent)
  88. ->getOneBySlug($requestAttributesArray['section']);
  89. if($sectionCurrent === null && $throwExceptionIfSectionNotFound){
  90. throw new NotFoundHttpException('Aucun espace n\'a été trouvé');
  91. }
  92. }
  93. /*
  94. * /!\ Cache désactivé car génération d'une erreur du type :
  95. * "A new entity was found through the relationship 'App\Entity\Order\OrderShop#section' that was not configured to cascade persist operations for entity: Marché"
  96. *
  97. * Arrive sur les pages générées via le cache HTTP (CacheController.php)
  98. */
  99. if ($sectionCurrent) {
  100. //$this->cacheSectionCurrent = $sectionCurrent;
  101. return $sectionCurrent;
  102. } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) {
  103. //$this->cacheSectionVisited = $currentVisitedSection;
  104. return $currentVisitedSection;
  105. } elseif ($returnDefaultIfOutOfSections && $sectionDefault) {
  106. //$this->cacheSectionDefault = $sectionDefault;
  107. return $sectionDefault;
  108. }
  109. return null;
  110. }
  111. }
  112. public function isOutOfSection(): bool
  113. {
  114. return $this->getCurrent(false, false, false) == null;
  115. }
  116. public function getDefault(): SectionInterface
  117. {
  118. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  119. }
  120. }