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.

113 line
3.5KB

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