|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- /**
- * @author La clic ! <contact@laclic.fr>
- */
-
-
- 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');
- }
- }
-
- }
|