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ů.

SectionResolver.php 5.2KB

před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if ($sectionCurrent) {
  94. $this->cacheSectionCurrent = $sectionCurrent;
  95. return $sectionCurrent;
  96. } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) {
  97. $this->cacheSectionVisited = $currentVisitedSection;
  98. return $currentVisitedSection;
  99. } elseif ($returnDefaultIfOutOfSections && $sectionDefault) {
  100. $this->cacheSectionDefault = $sectionDefault;
  101. return $sectionDefault;
  102. }
  103. return null;
  104. }
  105. }
  106. public function isOutOfSection(): bool
  107. {
  108. return $this->getCurrent(false, false, false) == null;
  109. }
  110. public function getDefault(): SectionInterface
  111. {
  112. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  113. }
  114. }