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.

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