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.

MerchantResolver.php 5.5KB

il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
il y a 3 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Definition\MerchantSettingDefinition;
  8. use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
  9. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  10. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  11. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  12. use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
  13. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  14. use Lc\CaracoleBundle\Repository\User\UserMerchantRepository;
  15. use Lc\SovBundle\Model\User\UserInterface;
  16. use Lc\SovBundle\Resolver\UrlResolver;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  20. use Symfony\Component\Security\Core\Security;
  21. class MerchantResolver
  22. {
  23. protected ?MerchantInterface $currentMerchant;
  24. protected RequestStack $requestStack;
  25. protected EntityManagerInterface $em;
  26. protected UrlResolver $urlResolver;
  27. protected Security $security;
  28. protected MerchantRepository $merchantRepository;
  29. protected UserMerchantRepository $userMerchantRepository;
  30. protected UrlGeneratorInterface $router;
  31. protected MerchantStore $merchantStore;
  32. protected ParameterBagInterface $parameterBag;
  33. public function __construct(
  34. EntityManagerInterface $entityManager,
  35. RequestStack $requestStack,
  36. UrlResolver $urlResolver,
  37. Security $security,
  38. MerchantRepository $merchantRepository,
  39. UserMerchantRepository $userMerchantRepository,
  40. UrlGeneratorInterface $router,
  41. MerchantStore $merchantStore,
  42. ParameterBagInterface $parameterBag
  43. ) {
  44. $this->requestStack = $requestStack;
  45. $this->em = $entityManager;
  46. $this->urlResolver = $urlResolver;
  47. $this->security = $security;
  48. $this->merchantRepository = $merchantRepository;
  49. $this->userMerchantRepository = $userMerchantRepository;
  50. $this->merchantStore = $merchantStore;
  51. $this->router = $router;
  52. $this->parameterBag = $parameterBag;
  53. }
  54. public function getCurrent(): MerchantInterface
  55. {
  56. if (isset($this->currentMerchant) && $this->currentMerchant) {
  57. return $this->currentMerchant;
  58. }
  59. $this->currentMerchant = null;
  60. $request = $this->requestStack->getCurrentRequest();
  61. $merchants = $this->merchantRepository->findAll();
  62. $isCli = php_sapi_name() === 'cli';
  63. if ($request || $isCli) {
  64. if ($isCli || $this->urlResolver->isServerLocalhost()) {
  65. foreach ($merchants as $merchant) {
  66. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
  67. $this->currentMerchant = $merchant;
  68. }
  69. }
  70. } // distant
  71. else {
  72. foreach ($merchants as $merchant) {
  73. $url = $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL);
  74. if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
  75. $this->currentMerchant = $merchant;
  76. }
  77. }
  78. }
  79. } else {
  80. /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) {
  81. foreach ($merchants as $merchant) {
  82. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) {
  83. $currentMerchant = $merchant;
  84. }
  85. }
  86. }*/
  87. }
  88. if ($this->currentMerchant instanceof MerchantInterface) {
  89. return $this->currentMerchant;
  90. } else {
  91. throw new \ErrorException('Aucun merchant courant');
  92. }
  93. }
  94. public function getUserMerchant(
  95. UserInterface $user = null,
  96. MerchantInterface $merchant = null
  97. ): ?UserMerchantInterface {
  98. if ($user === null) {
  99. $user = $this->security->getUser();
  100. }
  101. if ($merchant === null) {
  102. $merchant = $this->getCurrent();
  103. }
  104. return $this->userMerchantRepository->findOneBy(
  105. [
  106. 'user' => $user,
  107. 'merchant' => $merchant,
  108. ]
  109. );
  110. }
  111. public function getUrl(SectionInterface $section)
  112. {
  113. if ($this->urlResolver->isServerLocalhost()) {
  114. return $this->router->generate('frontend_home', ['section' => $section->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  115. } else {
  116. return $section->getMerchant()->getSettingValue(MerchantSettingDefinition::SETTING_URL);
  117. }
  118. }
  119. public function getMerchantUser(UserInterface $user = null)
  120. {
  121. if(is_null($user)) {
  122. $user = $this->security->getUser();
  123. }
  124. $merchants = $this->merchantStore->getOnline();
  125. if ($user) {
  126. return $user->getFavoriteMerchant();
  127. } else {
  128. $merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt(
  129. $this->parameterBag->get('app.cookie_name_merchant_current')
  130. );
  131. if ($merchantCurrentId) {
  132. foreach ($merchants as $merchant) {
  133. if ($merchant->getId() == $merchantCurrentId) {
  134. return $merchant;
  135. }
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. }