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.

MerchantResolver.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Model\Merchant\MerchantInterface;
  8. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  9. use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
  10. use Lc\SovBundle\Model\User\UserInterface;
  11. use Lc\SovBundle\Resolver\UrlResolver;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\Security\Core\Security;
  14. class MerchantResolver
  15. {
  16. protected $requestStack;
  17. protected $em;
  18. protected $urlResolver;
  19. protected $security;
  20. public function __construct(
  21. EntityManagerInterface $entityManager,
  22. RequestStack $requestStack,
  23. UrlResolver $urlResolver,
  24. Security $security
  25. ) {
  26. $this->requestStack = $requestStack;
  27. $this->em = $entityManager;
  28. $this->urlResolver = $urlResolver;
  29. $this->security = $security;
  30. }
  31. public function getCurrent(): MerchantInterface
  32. {
  33. /* if($currentMerchant) {
  34. return $currentMerchant ;
  35. }*/
  36. $currentMerchant = false;
  37. $request = $this->requestStack->getCurrentRequest();
  38. $merchantRepository = $this->em->getRepository(MerchantInterface::class);
  39. $merchants = $merchantRepository->findAll();
  40. if ($request) {
  41. if ($this->urlResolver->isServerLocalhost()) {
  42. foreach ($merchants as $merchant) {
  43. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
  44. $currentMerchant = $merchant;
  45. }
  46. }
  47. } // distant
  48. else {
  49. foreach ($merchants as $merchant) {
  50. $url = $merchant->getMerchantConfig('url');
  51. if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
  52. $currentMerchant = $merchant;
  53. }
  54. }
  55. }
  56. } else {
  57. /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) {
  58. foreach ($merchants as $merchant) {
  59. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) {
  60. $currentMerchant = $merchant;
  61. }
  62. }
  63. }*/
  64. }
  65. if ($currentMerchant instanceof MerchantInterface) {
  66. return $currentMerchant;
  67. } else {
  68. throw new \ErrorException('Aucun merchant courant');
  69. }
  70. }
  71. public function getUserMerchant(
  72. UserInterface $user = null,
  73. MerchantInterface $merchant = null
  74. ): ?UserMerchantInterface {
  75. $userMerchantRepository = $this->em->getRepository(UserMerchantInterface::class);
  76. if ($user === null) {
  77. $user = $this->security->getUser();
  78. }
  79. if ($merchant === null) {
  80. $merchant = $this->getCurrent();
  81. }
  82. return $userMerchantRepository->findOneBy(
  83. [
  84. 'user' => $user,
  85. 'merchant' => $merchant,
  86. ]
  87. );
  88. }
  89. }