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.

186 line
5.9KB

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