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.

91 satır
2.9KB

  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. $sectionCurrent = null;
  55. $isCli = php_sapi_name() === 'cli';
  56. // local
  57. if ($isCli || $this->urlResolver->isServerLocalhost()) {
  58. $sectionArray = $this->sectionStore
  59. ->setMerchant($this->merchantResolver->getCurrent())
  60. ->getOnline();
  61. foreach ($sectionArray as $section) {
  62. if ($section->getDevAlias() == $_ENV['CURRENT_SECTION_LOCAL']) {
  63. $sectionCurrent = $section;
  64. }
  65. }
  66. }
  67. // distant
  68. else {
  69. $sectionCurrent = $this->sectionStore
  70. ->setMerchant($this->merchantResolver->getCurrent())
  71. ->getOneDefault();
  72. }
  73. return $sectionCurrent;
  74. }
  75. }
  76. }