|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?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\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 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 = $this->settingSolver->getSettingValue($merchant, 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();
- }
-
- //TODO Pas de REPO !!!!!
- return $this->userMerchantRepository->findOneBy(
- [
- 'user' => $user,
- 'merchant' => $merchant,
- ]
- );
- }
-
- public function getUrl(SectionInterface $section)
- {
- $url = $this->settingSolver->getSettingValue($section->getMerchant(), MerchantSettingDefinition::SETTING_URL);
-
- if(!$section->getIsDefault()) {
- $url .= $section->getSlug();
- }
-
- return$url;
- }
-
- public function getMerchantUser(UserInterface $user = null)
- {
- if(is_null($user)) {
- $user = $this->security->getUser();
- }
-
- $merchants = $this->merchantStore->getOnline();
-
- if ($user) {
- return $user->getFavoriteMerchant();
- } 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;
- }
-
- }
|