Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SectionResolver.php 4.4KB

3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\Security\Core\Security;
  13. class SectionResolver
  14. {
  15. protected bool $isCachedSection = false;
  16. protected ?SectionInterface $section = null;
  17. // cache front
  18. protected ?SectionInterface $cacheSectionCurrent = null;
  19. protected ?SectionInterface $cacheSectionVisited = null;
  20. protected ?SectionInterface $cacheSectionDefault = null;
  21. protected EntityManagerInterface $entityManager;
  22. protected Security $security;
  23. protected MerchantResolver $merchantResolver;
  24. protected SectionStore $sectionStore;
  25. protected RequestStack $requestStack;
  26. protected UrlResolver $urlResolver;
  27. public function __construct(
  28. EntityManagerInterface $entityManager,
  29. Security $security,
  30. MerchantResolver $merchantResolver,
  31. SectionStore $sectionStore,
  32. RequestStack $requestStack,
  33. UrlResolver $urlResolver
  34. ) {
  35. $this->entityManager = $entityManager;
  36. $this->security = $security;
  37. $this->merchantResolver = $merchantResolver;
  38. $this->sectionStore = $sectionStore;
  39. $this->requestStack = $requestStack;
  40. $this->urlResolver = $urlResolver;
  41. }
  42. public function getCurrent($returnDefaultIfOutOfSections = false, $returnVisitedIfOutOfSection = false)
  43. {
  44. $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();
  45. // admin
  46. if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') {
  47. if (!$this->isCachedSection) {
  48. $currentAdminSection = null;
  49. $userMerchant = $this->merchantResolver->getUserMerchant();
  50. if ($userMerchant !== null) {
  51. $currentAdminSection = $userMerchant->getCurrentAdminSection();
  52. }
  53. $this->isCachedSection = true;
  54. $this->section = $currentAdminSection;
  55. return $currentAdminSection;
  56. } else {
  57. return $this->section;
  58. }
  59. } // front
  60. else {
  61. if ($this->cacheSectionCurrent) {
  62. return $this->cacheSectionCurrent;
  63. } elseif ($returnVisitedIfOutOfSection && $this->cacheSectionVisited) {
  64. return $this->cacheSectionVisited;
  65. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  66. return $this->cacheSectionDefault;
  67. }
  68. $userCurrent = $this->security->getUser();
  69. $merchantCurrent = $this->merchantResolver->getCurrent();
  70. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  71. $sectionCurrent = null;
  72. $sectionDefault = $sectionStore->getOneDefault();
  73. $currentVisitedSection = null;
  74. if ($userCurrent && $userCurrent->getCurrentVisitedSection()) {
  75. $currentVisitedSection = $userCurrent->getCurrentVisitedSection();
  76. }
  77. if (isset($requestAttributesArray['section'])) {
  78. $sectionCurrent = $sectionStore
  79. ->setMerchant($merchantCurrent)
  80. ->getOneBySlug($requestAttributesArray['section']);
  81. }
  82. if ($sectionCurrent) {
  83. $this->cacheSectionCurrent = $sectionCurrent;
  84. return $sectionCurrent;
  85. } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) {
  86. $this->cacheSectionVisited = $currentVisitedSection;
  87. return $currentVisitedSection;
  88. } elseif ($returnDefaultIfOutOfSections && $sectionDefault) {
  89. $this->cacheSectionDefault = $sectionDefault;
  90. return $sectionDefault;
  91. }
  92. return null;
  93. }
  94. }
  95. public function isOutOfSection(): bool
  96. {
  97. return $this->getCurrent() == null;
  98. }
  99. public function getDefault(): SectionInterface
  100. {
  101. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  102. }
  103. }