Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MerchantResolver.php 3.4KB

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