No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

85 líneas
2.8KB

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