|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * @author La clic ! <contact@laclic.fr>
- */
-
-
- namespace Lc\CaracoleBundle\Resolver;
-
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Resolver\UrlResolver;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\Security\Core\Security;
-
- class MerchantResolver
- {
- protected $requestStack;
- protected $em;
- protected $urlResolver;
- protected $security;
- protected $currentMerchant;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlResolver $urlResolver,
- Security $security
- ) {
- $this->requestStack = $requestStack;
- $this->em = $entityManager;
- $this->urlResolver = $urlResolver;
- $this->security = $security;
- }
-
- public function getCurrent(): MerchantInterface
- {
- if ($this->currentMerchant) {
- return $this->currentMerchant;
- }
-
- $this->currentMerchant = false;
- $request = $this->requestStack->getCurrentRequest();
- $merchantRepository = $this->em->getRepository(MerchantInterface::class);
- $merchants = $merchantRepository->findAll();
-
- if ($request) {
- if ($this->urlResolver->isServerLocalhost()) {
- foreach ($merchants as $merchant) {
- if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_LOCAL']) {
- $this->currentMerchant = $merchant;
- }
- }
- } // distant
- else {
- foreach ($merchants as $merchant) {
- $url = $merchant->getMerchantConfig('url');
- if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
- $this->currentMerchant = $merchant;
- }
- }
- }
- } else {
- /*if ($_ENV['CURRENT_MERCHANT_COMMAND']) {
- foreach ($merchants as $merchant) {
- if ($merchant->getId() == $_ENV['CURRENT_MERCHANT_COMMAND']) {
- $currentMerchant = $merchant;
- }
- }
- }*/
- }
-
- if ($this->currentMerchant instanceof MerchantInterface) {
- return $this->currentMerchant;
- } else {
- throw new \ErrorException('Aucun merchant courant');
- }
- }
-
-
- public function getUserMerchant(
- UserInterface $user = null,
- MerchantInterface $merchant = null
- ): ?UserMerchantInterface {
- $userMerchantRepository = $this->em->getRepository(UserMerchantInterface::class);
-
- if ($user === null) {
- $user = $this->security->getUser();
- }
- if ($merchant === null) {
- $merchant = $this->getCurrent();
- }
-
- return $userMerchantRepository->findOneBy(
- [
- 'user' => $user,
- 'merchant' => $merchant,
- ]
- );
- }
-
- }
|