Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

105 linhas
3.1KB

  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. public function __construct(
  21. EntityManagerInterface $entityManager,
  22. RequestStack $requestStack,
  23. UrlResolver $urlResolver,
  24. Security $security
  25. ) {
  26. $this->requestStack = $requestStack;
  27. $this->em = $entityManager;
  28. $this->urlResolver = $urlResolver;
  29. $this->security = $security;
  30. }
  31. public function getCurrent(): MerchantInterface
  32. {
  33. /* if($currentMerchant) {
  34. return $currentMerchant ;
  35. }*/
  36. $currentMerchant = false;
  37. $request = $this->requestStack->getCurrentRequest();
  38. $merchantRepository = $this->em->getRepository(MerchantInterface::class);
  39. $merchants = $merchantRepository->findAll();
  40. if ($request) {
  41. if ($this->urlResolver->isServerLocalhost()) {
  42. foreach ($merchants as $merchant) {
  43. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
  44. $currentMerchant = $merchant;
  45. }
  46. }
  47. } // distant
  48. else {
  49. foreach ($merchants as $merchant) {
  50. $url = $merchant->getMerchantConfig('url');
  51. if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
  52. $currentMerchant = $merchant;
  53. }
  54. }
  55. }
  56. } else {
  57. /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) {
  58. foreach ($merchants as $merchant) {
  59. if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) {
  60. $currentMerchant = $merchant;
  61. }
  62. }
  63. }*/
  64. }
  65. if ($currentMerchant instanceof MerchantInterface) {
  66. return $currentMerchant;
  67. } else {
  68. throw new \ErrorException('Aucun merchant courant');
  69. }
  70. }
  71. public function getUserMerchant(
  72. UserInterface $user = null,
  73. MerchantInterface $merchant = null
  74. ): ?UserMerchantInterface {
  75. $userMerchantRepository = $this->em->getRepository(UserMerchantInterface::class);
  76. if ($user === null) {
  77. $user = $this->security->getUser();
  78. }
  79. if ($merchant === null) {
  80. $merchant = $this->getCurrent();
  81. }
  82. return $userMerchantRepository->findOneBy(
  83. [
  84. 'user' => $user,
  85. 'merchant' => $merchant,
  86. ]
  87. );
  88. }
  89. }