|
- <?php
- /**
- * @author La clic ! <contact@laclic.fr>
- */
-
-
- namespace Lc\CaracoleBundle\Resolver;
-
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinition;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\CaracoleBundle\Repository\User\UserMerchantRepository;
- use Lc\SovBundle\Model\User\UserInterface;
- use Lc\SovBundle\Resolver\UrlResolver;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
- use Symfony\Component\Security\Core\Security;
-
- class MerchantResolver
- {
- protected ?MerchantInterface $currentMerchant;
- protected RequestStack $requestStack;
- protected EntityManagerInterface $em;
- protected UrlResolver $urlResolver;
- protected Security $security;
- protected MerchantRepository $merchantRepository;
- protected UserMerchantRepository $userMerchantRepository;
- protected UrlGeneratorInterface $router;
- protected MerchantStore $merchantStore;
- protected ParameterBagInterface $parameterBag;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlResolver $urlResolver,
- Security $security,
- MerchantRepository $merchantRepository,
- UserMerchantRepository $userMerchantRepository,
- UrlGeneratorInterface $router,
- MerchantStore $merchantStore,
- ParameterBagInterface $parameterBag
- ) {
- $this->requestStack = $requestStack;
- $this->em = $entityManager;
- $this->urlResolver = $urlResolver;
- $this->security = $security;
- $this->merchantRepository = $merchantRepository;
- $this->userMerchantRepository = $userMerchantRepository;
- $this->merchantStore = $merchantStore;
- $this->router = $router;
- $this->parameterBag = $parameterBag;
- }
-
- public function getCurrent(): MerchantInterface
- {
- if (isset($this->currentMerchant) && $this->currentMerchant) {
- return $this->currentMerchant;
- }
-
- $this->currentMerchant = null;
- $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->getSettingValue(MerchantSettingDefinition::SETTING_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,
- ]
- );
- }
-
- public function getUrl($merchant)
- {
- if ($this->urlResolver->isServerLocalhost()) {
- return $this->router->generate('frontend_home', [], UrlGeneratorInterface::ABSOLUTE_URL);
- } else {
- return $merchant->getSettingValue(MerchantSettingDefinition::SETTING_URL);
- }
- }
-
- public function getMerchantUser(UserInterface $user = null)
- {
- $merchants = $this->merchantStore->findAll();
-
- if ($user) {
- return $user->getMerchant();
- } else {
- $merchantCurrentId = $this->requestStack->getCurrentRequest()->cookies->getInt(
- $this->parameterBag->get('app.cookie_name_merchant_current')
- );
- if ($merchantCurrentId) {
- foreach ($merchants as $merchant) {
- if ($merchant->getId() == $merchantCurrentId) {
- return $merchant;
- }
- }
- }
- }
-
- return false;
- }
-
- }
|