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.

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