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.

107 lines
3.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\Security\Core\Security;
  13. class SectionResolver
  14. {
  15. protected bool $isCachedSection = false;
  16. protected ?SectionInterface $section = null;
  17. protected EntityManagerInterface $entityManager;
  18. protected MerchantResolver $merchantResolver;
  19. protected SectionStore $sectionStore;
  20. protected RequestStack $requestStack;
  21. protected UrlResolver $urlResolver;
  22. public function __construct(
  23. EntityManagerInterface $entityManager,
  24. MerchantResolver $merchantResolver,
  25. SectionStore $sectionStore,
  26. RequestStack $requestStack,
  27. UrlResolver $urlResolver
  28. )
  29. {
  30. $this->entityManager = $entityManager;
  31. $this->merchantResolver = $merchantResolver;
  32. $this->sectionStore = $sectionStore;
  33. $this->requestStack = $requestStack;
  34. $this->urlResolver = $urlResolver;
  35. }
  36. public function getCurrent()
  37. {
  38. $requestAttributesArray = $this->requestStack->getMainRequest()->attributes->all();
  39. // admin
  40. if (isset($requestAttributesArray['_firewall_context']) && $requestAttributesArray['_firewall_context'] == 'security.firewall.map.context.admin') {
  41. //dump($requestAttributesArray);
  42. if (!$this->isCachedSection) {
  43. $currentAdminSection = null;
  44. $userMerchant = $this->merchantResolver->getUserMerchant();
  45. if ($userMerchant !== null) {
  46. $currentAdminSection = $userMerchant->getCurrentAdminSection();
  47. }
  48. /*if ($currentAdminSection === null) {
  49. $currentAdminSection = $this->sectionStore
  50. ->setMerchant($userMerchant->getMerchant())
  51. ->getOneDefault();
  52. if ($currentAdminSection === null) {
  53. throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
  54. }
  55. }*/
  56. $this->isCachedSection = true;
  57. $this->section = $currentAdminSection;
  58. return $currentAdminSection;
  59. }else{
  60. return $this->section;
  61. }
  62. } // front
  63. else {
  64. if($this->section === null) {
  65. $merchantCurrent = $this->merchantResolver->getCurrent();
  66. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  67. $sectionCurrent = null;
  68. $sectionDefault = $sectionStore->getOneDefault();
  69. if (isset($requestAttributesArray['section'])) {
  70. $sectionCurrent = $sectionStore
  71. ->setMerchant($merchantCurrent)
  72. ->getOneBySlug($requestAttributesArray['section']);
  73. }
  74. $this->section = $sectionCurrent ?: $sectionDefault;
  75. }
  76. return $this->section;
  77. }
  78. }
  79. public function isOutOfSection(): bool
  80. {
  81. return $this->getCurrent() == null;
  82. }
  83. public function getDefault(): SectionInterface
  84. {
  85. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  86. }
  87. }