選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

208 行
6.8KB

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