*/ namespace Lc\CaracoleBundle\Resolver; use Doctrine\ORM\EntityManagerInterface; use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; use Lc\SovBundle\Resolver\UrlResolver; use Symfony\Component\HttpFoundation\RequestStack; class MerchantResolver { protected $requestStack; protected $em; protected $urlResolver; public function __construct(EntityManagerInterface $entityManager, RequestStack $requestStack, UrlResolver $urlResolver) { $this->requestStack = $requestStack; $this->em = $entityManager; $this->urlResolver = $urlResolver; } public function getCurrent(): MerchantInterface { /* if($currentMerchant) { return $currentMerchant ; }*/ $currentMerchant = false; $request = $this->requestStack->getCurrentRequest(); $merchantRepo = $this->em->getRepository(MerchantInterface::class); $merchants = $merchantRepo->findAll(); if ($request) { if ($this->urlResolver->isServerLocalhost()) { foreach ($merchants as $merchant) { if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) { $currentMerchant = $merchant; } } } // distant else { foreach ($merchants as $merchant) { $url = $merchant->getMerchantConfig('url'); if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) { $currentMerchant = $merchant; } } } } else { /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) { foreach ($merchants as $merchant) { if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) { $currentMerchant = $merchant; } } }*/ } if ($currentMerchant instanceof MerchantInterface) { return $currentMerchant; } else { throw new \ErrorException('Aucun merchant courant'); } } }