You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = [];
  46. $request = $this->requestStack->getMainRequest();
  47. if($request) {
  48. $requestAttributesArray = $request->attributes->all();
  49. }
  50. // admin
  51. if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') {
  52. if ($this->cacheSectionCurrent) {
  53. return $this->cacheSectionCurrent;
  54. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  55. return $this->cacheSectionDefault;
  56. }
  57. $sectionCurrent = null;
  58. $userMerchant = $this->merchantResolver->getUserMerchant();
  59. if($userMerchant !== null) {
  60. $sectionCurrent = $userMerchant->getCurrentAdminSection();
  61. }
  62. $sectionDefault = $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  63. if($sectionCurrent) {
  64. $this->cacheSectionCurrent = $sectionCurrent;
  65. return $sectionCurrent;
  66. } elseif($returnDefaultIfOutOfSections && $sectionDefault) {
  67. $this->cacheSectionDefault = $sectionDefault;
  68. return $sectionDefault;
  69. }
  70. return null;
  71. } // front
  72. else {
  73. if ($this->cacheSectionCurrent) {
  74. return $this->cacheSectionCurrent;
  75. } elseif ($returnVisitedIfOutOfSection && $this->cacheSectionVisited) {
  76. return $this->cacheSectionVisited;
  77. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  78. return $this->cacheSectionDefault;
  79. }
  80. $userCurrent = $this->security->getUser();
  81. $merchantCurrent = $this->merchantResolver->getCurrent();
  82. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  83. $sectionCurrent = null;
  84. $sectionDefault = $sectionStore->getOneDefault();
  85. $currentVisitedSection = null;
  86. if ($userCurrent && $userCurrent->getCurrentVisitedSection()) {
  87. $currentVisitedSection = $userCurrent->getCurrentVisitedSection();
  88. }
  89. if (isset($requestAttributesArray['section'])) {
  90. $sectionCurrent = $sectionStore
  91. ->setMerchant($merchantCurrent)
  92. ->getOneBySlug($requestAttributesArray['section']);
  93. if($sectionCurrent === null && $throwExceptionIfSectionNotFound){
  94. throw new NotFoundHttpException('Aucun espace n\'a été trouvé');
  95. }
  96. }
  97. /*
  98. * /!\ Cache désactivé car génération d'une erreur du type :
  99. * "A new entity was found through the relationship 'App\Entity\Order\OrderShop#section' that was not configured to cascade persist operations for entity: Marché"
  100. *
  101. * Arrive sur les pages générées via le cache HTTP (CacheController.php)
  102. */
  103. if ($sectionCurrent) {
  104. //$this->cacheSectionCurrent = $sectionCurrent;
  105. return $sectionCurrent;
  106. } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) {
  107. //$this->cacheSectionVisited = $currentVisitedSection;
  108. return $currentVisitedSection;
  109. } elseif ($returnDefaultIfOutOfSections && $sectionDefault) {
  110. //$this->cacheSectionDefault = $sectionDefault;
  111. return $sectionDefault;
  112. }
  113. return null;
  114. }
  115. }
  116. public function isOutOfSection(): bool
  117. {
  118. return $this->getCurrent(false, false, false) == null;
  119. }
  120. public function getDefault(): SectionInterface
  121. {
  122. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  123. }
  124. }