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.

SectionResolver.php 3.4KB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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['easyadmin_context'])) {
  41. if (!$this->isCachedSection) {
  42. $currentAdminSection = null;
  43. $userMerchant = $this->merchantResolver->getUserMerchant();
  44. if ($userMerchant !== null) {
  45. $currentAdminSection = $userMerchant->getCurrentAdminSection();
  46. }
  47. /*if ($currentAdminSection === null) {
  48. $currentAdminSection = $this->sectionStore
  49. ->setMerchant($userMerchant->getMerchant())
  50. ->getOneDefault();
  51. if ($currentAdminSection === null) {
  52. throw new \ErrorException('Aucune section par défaut définie pour ce merchant');
  53. }
  54. }*/
  55. $this->isCachedSection = true;
  56. $this->section = $currentAdminSection;
  57. return $currentAdminSection;
  58. }else{
  59. return $this->section;
  60. }
  61. } // front
  62. else {
  63. if($this->section === null) {
  64. $merchantCurrent = $this->merchantResolver->getCurrent();
  65. $sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
  66. $sectionCurrent = null;
  67. $sectionDefault = $sectionStore->getOneDefault();
  68. if (isset($requestAttributesArray['section'])) {
  69. $sectionCurrent = $sectionStore
  70. ->setMerchant($merchantCurrent)
  71. ->getOneBySlug($requestAttributesArray['section']);
  72. }
  73. $this->section = $sectionCurrent ?: $sectionDefault;
  74. }
  75. return $this->section;
  76. }
  77. }
  78. public function isOutOfSection(): bool
  79. {
  80. return $this->getCurrent() == null;
  81. }
  82. public function getDefault(): SectionInterface
  83. {
  84. return $this->sectionStore->setMerchant($this->merchantResolver->getCurrent())->getOneDefault();
  85. }
  86. }