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.

108 line
3.7KB

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