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.

107 line
3.2KB

  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. $isCli = php_sapi_name() === 'cli';
  42. if ($request || $isCli) {
  43. if ($isCli || $this->urlResolver->isServerLocalhost()) {
  44. foreach ($merchants as $merchant) {
  45. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
  46. $this->currentMerchant = $merchant;
  47. }
  48. }
  49. } // distant
  50. else {
  51. foreach ($merchants as $merchant) {
  52. $url = $merchant->getMerchantConfig('url');
  53. if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
  54. $this->currentMerchant = $merchant;
  55. }
  56. }
  57. }
  58. } else {
  59. /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) {
  60. foreach ($merchants as $merchant) {
  61. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) {
  62. $currentMerchant = $merchant;
  63. }
  64. }
  65. }*/
  66. }
  67. if ($this->currentMerchant instanceof MerchantInterface) {
  68. return $this->currentMerchant;
  69. } else {
  70. throw new \ErrorException('Aucun merchant courant');
  71. }
  72. }
  73. public function getUserMerchant(
  74. UserInterface $user = null,
  75. MerchantInterface $merchant = null
  76. ): ?UserMerchantInterface {
  77. $userMerchantRepository = $this->em->getRepository(UserMerchantInterface::class);
  78. if ($user === null) {
  79. $user = $this->security->getUser();
  80. }
  81. if ($merchant === null) {
  82. $merchant = $this->getCurrent();
  83. }
  84. return $userMerchantRepository->findOneBy(
  85. [
  86. 'user' => $user,
  87. 'merchant' => $merchant,
  88. ]
  89. );
  90. }
  91. }