|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?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;
- }
-
- }
|