Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MerchantResolver.php 3.2KB

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