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.

96 line
3.2KB

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