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.

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