Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

203 lines
6.5KB

  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. }