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.6KB

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