No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MerchantResolver.php 6.5KB

hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
hace 3 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 isOutOfMerchant()
  59. {
  60. return !$this->getCurrentProtected();
  61. }
  62. public function getCurrent(): MerchantInterface
  63. {
  64. if (isset($this->currentMerchant) && $this->currentMerchant) {
  65. return $this->currentMerchant;
  66. }
  67. $this->currentMerchant = null;
  68. $merchant = $this->getCurrentProtected();
  69. if($merchant) {
  70. $this->currentMerchant = $merchant;
  71. }
  72. if ($this->currentMerchant instanceof MerchantInterface) {
  73. return $this->currentMerchant;
  74. }
  75. else {
  76. throw new \ErrorException('Aucun merchant courant');
  77. }
  78. }
  79. protected function getCurrentProtected(): ?MerchantInterface
  80. {
  81. $currentMerchant = null;
  82. $request = $this->requestStack->getCurrentRequest();
  83. $merchants = $this->merchantRepository->findAll();
  84. $isCli = php_sapi_name() === 'cli';
  85. if ($request || $isCli) {
  86. if ($isCli || $this->urlResolver->isServerLocalhost()) {
  87. foreach ($merchants as $merchant) {
  88. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
  89. $currentMerchant = $merchant;
  90. }
  91. }
  92. } // distant
  93. else {
  94. foreach ($merchants as $merchant) {
  95. $url = $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL);
  96. if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
  97. $currentMerchant = $merchant;
  98. }
  99. }
  100. }
  101. }
  102. return $currentMerchant;
  103. }
  104. public function getUserMerchant(
  105. UserInterface $user = null,
  106. MerchantInterface $merchant = null
  107. ): ?UserMerchantInterface {
  108. if ($user === null) {
  109. $user = $this->security->getUser();
  110. }
  111. if ($merchant === null) {
  112. $merchant = $this->getCurrent();
  113. }
  114. // @TODO Pas de REPO !!!!!
  115. return $this->userMerchantRepository->findOneBy(
  116. [
  117. 'user' => $user,
  118. 'merchant' => $merchant,
  119. ]
  120. );
  121. }
  122. public function getAbsoluteUrl(MerchantInterface $merchant, string $name, array $parameters = []): string
  123. {
  124. $url = $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL);
  125. if(substr($url, strlen($url) - 1, 1) == '/') {
  126. $url = substr($url, 0, strlen($url) - 1);
  127. }
  128. return $url . $this->router->generate($name, $parameters);
  129. }
  130. public function getUrl(SectionInterface $section)
  131. {
  132. $url = $this->settingSolver->getSettingValue($section->getMerchant(), MerchantSettingDefinition::SETTING_URL);
  133. if(!$section->getIsDefault()) {
  134. $url .= $section->getSlug();
  135. }
  136. return $url;
  137. }
  138. public function getUrlAdmin(MerchantInterface $merchant)
  139. {
  140. return $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL).'admin';
  141. }
  142. public function getMerchantUser(UserInterface $user = null)
  143. {
  144. if(is_null($user)) {
  145. $user = $this->security->getUser();
  146. }
  147. if ($user) {
  148. return $user->getFavoriteMerchant();
  149. }
  150. else {
  151. $merchantCurrent = $this->getMerchantUserViaCookie();
  152. if($merchantCurrent) {
  153. return $merchantCurrent;
  154. }
  155. }
  156. return false;
  157. }
  158. public function getMerchantUserViaCookie()
  159. {
  160. $merchants = $this->merchantStore->getOnline();
  161. $merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt(
  162. $this->parameterBag->get('app.cookie_name_merchant_current')
  163. );
  164. if ($merchantCurrentId) {
  165. foreach ($merchants as $merchant) {
  166. if ($merchant->getId() == $merchantCurrentId) {
  167. return $merchant;
  168. }
  169. }
  170. }
  171. return null;
  172. }
  173. }