您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

166 行
5.7KB

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