Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

112 lines
3.4KB

  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. }