Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

129 Zeilen
4.6KB

  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)
  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->isCachedSection) {
  49. $currentAdminSection = null;
  50. $userMerchant = $this->merchantResolver->getUserMerchant();
  51. if ($userMerchant !== null) {
  52. $currentAdminSection = $userMerchant->getCurrentAdminSection();
  53. }
  54. $this->isCachedSection = true;
  55. $this->section = $currentAdminSection;
  56. return $currentAdminSection;
  57. } else {
  58. return $this->section;
  59. }
  60. } // front
  61. else {
  62. if ($this->cacheSectionCurrent) {
  63. return $this->cacheSectionCurrent;
  64. } elseif ($returnVisitedIfOutOfSection && $this->cacheSectionVisited) {
  65. return $this->cacheSectionVisited;
  66. } elseif ($returnDefaultIfOutOfSections && $this->cacheSectionDefault) {
  67. return $this->cacheSectionDefault;
  68. }
  69. $userCurrent = $this->security->getUser();
  70. $merchantCurrent = $this->merchantResolver->getCurrent();
  71. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  72. $sectionCurrent = null;
  73. $sectionDefault = $sectionStore->getOneDefault();
  74. $currentVisitedSection = null;
  75. if ($userCurrent && $userCurrent->getCurrentVisitedSection()) {
  76. $currentVisitedSection = $userCurrent->getCurrentVisitedSection();
  77. }
  78. if (isset($requestAttributesArray['section'])) {
  79. $sectionCurrent = $sectionStore
  80. ->setMerchant($merchantCurrent)
  81. ->getOneBySlug($requestAttributesArray['section']);
  82. if($sectionCurrent===null){
  83. throw new NotFoundHttpException('Aucun espace n\'a été trouvé');
  84. }
  85. }
  86. if ($sectionCurrent) {
  87. $this->cacheSectionCurrent = $sectionCurrent;
  88. return $sectionCurrent;
  89. } elseif ($returnVisitedIfOutOfSection && $currentVisitedSection) {
  90. $this->cacheSectionVisited = $currentVisitedSection;
  91. return $currentVisitedSection;
  92. } elseif ($returnDefaultIfOutOfSections && $sectionDefault) {
  93. $this->cacheSectionDefault = $sectionDefault;
  94. return $sectionDefault;
  95. }
  96. return null;
  97. }
  98. }
  99. public function isOutOfSection(): bool
  100. {
  101. return $this->getCurrent() == null;
  102. }
  103. public function getDefault(): SectionInterface
  104. {
  105. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  106. }
  107. }