|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?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\CaracoleBundle\Repository\User\UserMerchantRepository;
- 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;
- protected $merchantRepository;
- protected $userMerrchantRepository;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlResolver $urlResolver,
- Security $security,
- MerchantRepository $merchantRepository,
- UserMerchantRepository $userMerchantRepository
- ) {
- $this->requestStack = $requestStack;
- $this->em = $entityManager;
- $this->urlResolver = $urlResolver;
- $this->security = $security;
- $this->merchantRepository = $merchantRepository;
- $this->userMerchantRepository = $userMerchantRepository;
- }
-
- public function getCurrent(): MerchantInterface
- {
- if ($this->currentMerchant) {
- return $this->currentMerchant;
- }
-
- $this->currentMerchant = false;
- $request = $this->requestStack->getCurrentRequest();
- $merchants = $this->merchantRepository->findAll();
- $isCli = php_sapi_name() === 'cli';
-
- if ($request || $isCli) {
- if ($isCli || $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 {
-
- if ($user === null) {
- $user = $this->security->getUser();
- }
- if ($merchant === null) {
- $merchant = $this->getCurrent();
- }
-
- return $this->userMerchantRepository->findOneBy(
- [
- 'user' => $user,
- 'merchant' => $merchant,
- ]
- );
- }
-
- }
|