|
- <?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\Section\SectionInterface;
- 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 Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
- use Symfony\Component\HttpFoundation\RequestStack;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- 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;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- RequestStack $requestStack,
- UrlResolver $urlResolver,
- Security $security,
- MerchantRepository $merchantRepository,
- UserMerchantRepository $userMerchantRepository,
- UrlGeneratorInterface $router,
- MerchantStore $merchantStore,
- ParameterBagInterface $parameterBag,
- SettingSolver $settingSolver
- ) {
- $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;
- $this->settingSolver = $settingSolver;
- }
-
- public function isOutOfMerchant()
- {
- return !$this->getCurrentProtected();
- }
-
- public function getCurrent(): MerchantInterface
- {
- if (isset($this->currentMerchant) && $this->currentMerchant) {
- return $this->currentMerchant;
- }
-
- $this->currentMerchant = null;
- $merchant = $this->getCurrentProtected();
-
- if($merchant) {
- $this->currentMerchant = $merchant;
- }
-
- if ($this->currentMerchant instanceof MerchantInterface) {
- return $this->currentMerchant;
- }
- else {
- throw new NotFoundHttpException('Aucun marchand n\'est défini');
- }
- }
-
- protected function getCurrentProtected(): ?MerchantInterface
- {
- $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']) {
- $currentMerchant = $merchant;
- }
- }
- } // distant
- else {
- foreach ($merchants as $merchant) {
- $url = $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL);
- if ($url && strlen($url) && strpos($url, $_SERVER['HTTP_HOST']) !== false) {
- $currentMerchant = $merchant;
- }
- }
- }
- }
-
- return $currentMerchant;
- }
-
- public function getUserMerchant(
- UserInterface $user = null,
- MerchantInterface $merchant = null
- ): ?UserMerchantInterface {
- if ($user === null) {
- $user = $this->security->getUser();
- }
- if ($merchant === null) {
- $merchant = $this->getCurrent();
- }
-
- // @TODO Pas de REPO !!!!!
- return $this->userMerchantRepository->findOneBy(
- [
- 'user' => $user,
- 'merchant' => $merchant,
- ]
- );
- }
-
- public function getAbsoluteUrl(MerchantInterface $merchant, string $name, array $parameters = []): string
- {
- $url = $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL);
-
- if(substr($url, strlen($url) - 1, 1) == '/') {
- $url = substr($url, 0, strlen($url) - 1);
- }
-
- return $url . $this->router->generate($name, $parameters);
- }
-
- public function getUrl(SectionInterface $section)
- {
- $url = $this->settingSolver->getSettingValue($section->getMerchant(), MerchantSettingDefinition::SETTING_URL);
-
- if(!$section->getIsDefault()) {
- $url .= $section->getSlug();
- }
-
- return $url;
- }
-
- public function getUrlAdmin(MerchantInterface $merchant)
- {
- return $this->settingSolver->getSettingValue($merchant, MerchantSettingDefinition::SETTING_URL).'admin';
- }
-
- public function getMerchantUser(UserInterface $user = null)
- {
- if(is_null($user)) {
- $user = $this->security->getUser();
- }
-
- if ($user) {
- return $user->getFavoriteMerchant();
- }
- else {
- $merchantCurrent = $this->getMerchantUserViaCookie();
- if($merchantCurrent) {
- return $merchantCurrent;
- }
- }
-
- return false;
- }
-
- public function getMerchantUserViaCookie()
- {
- $merchants = $this->merchantStore->getOnline();
- $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 null;
- }
-
- }
|