Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

160 lines
5.6KB

  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. //TODO Pas de REPO !!!!!
  105. return $this->userMerchantRepository->findOneBy(
  106. [
  107. 'user' => $user,
  108. 'merchant' => $merchant,
  109. ]
  110. );
  111. }
  112. public function getUrl(SectionInterface $section)
  113. {
  114. if ($this->urlResolver->isServerLocalhost()) {
  115. return $this->router->generate('frontend_home', ['section' => $section->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  116. } else {
  117. return $section->getMerchant()->getSettingValue(MerchantSettingDefinition::SETTING_URL);
  118. }
  119. }
  120. public function getMerchantUser(UserInterface $user = null)
  121. {
  122. if(is_null($user)) {
  123. $user = $this->security->getUser();
  124. }
  125. $merchants = $this->merchantStore->getOnline();
  126. if ($user) {
  127. return $user->getFavoriteMerchant();
  128. } else {
  129. $merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt(
  130. $this->parameterBag->get('app.cookie_name_merchant_current')
  131. );
  132. if ($merchantCurrentId) {
  133. foreach ($merchants as $merchant) {
  134. if ($merchant->getId() == $merchantCurrentId) {
  135. return $merchant;
  136. }
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. }