Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

77 lines
2.5KB

  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. $sectionStore = $this->sectionStore->setMerchant($this->merchantResolver->getCurrent());
  55. $sectionCurrent = null;
  56. $sectionDefault = $sectionStore->getOneDefault();
  57. if(isset($requestAttributesArray['section'])) {
  58. $sectionCurrent = $sectionStore->getOneBySlug($requestAttributesArray['section']);
  59. }
  60. return $sectionCurrent ?: $sectionDefault;
  61. }
  62. }
  63. }