src/Service/AdvertisingManagement.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Advertiser;
  4. use App\Repository\AdvertiserRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Yaml\Yaml;
  9. class AdvertisingManagement
  10. {
  11.     private AdvertiserRepository $advertiserRepository;
  12.     private EntityManagerInterface $entityManager;
  13.     private RequestStack $requestStack;
  14.     private ?Advertiser $currentAdvertiser null;
  15.     private ?Advertiser $defaultAdvertiser null;
  16.     private string $staciSubDomain;
  17.     private string $cookieDomain;
  18.     public function __construct(AdvertiserRepository $advertiserRepositoryEntityManagerInterface $entityManagerRequestStack $requestStackstring $staciSubDomainstring $cookieDomain)
  19.     {
  20.         $this->advertiserRepository $advertiserRepository;
  21.         $this->entityManager $entityManager;
  22.         $this->requestStack $requestStack;
  23.         $this->staciSubDomain $staciSubDomain;
  24.         $this->cookieDomain $cookieDomain;
  25.     }
  26.     public function getAbsoluteUrl(string $subdomain): string
  27.     {
  28.         $defaultAdvertiser $this->getDefaultAdvertiser();
  29.         $url '';
  30.         if ($subdomain === $defaultAdvertiser->getSubdomain()) {
  31.             $url preg_replace('!^.!'''$this->cookieDomain);
  32.         } else {
  33.             $url $subdomain $this->cookieDomain;
  34.         }
  35.         if ($this->requestStack->getCurrentRequest() === null || empty($this->requestStack->getCurrentRequest()->server->get('REQUEST_SCHEME')) || $this->requestStack->getCurrentRequest()->server->get('HTTPS') === 'on') {
  36.             $scheme 'https';
  37.         } else {
  38.             $scheme $this->requestStack->getCurrentRequest()->server->get('REQUEST_SCHEME');
  39.         }
  40.         return $scheme '://' $url;
  41.     }
  42.     public function getSubdomain(): string
  43.     {
  44.         if ($this->requestStack->getCurrentRequest() instanceof Request && !empty($this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'))) {
  45.             $host explode('.', (string) $this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'));
  46.             $subdomain $host[0];
  47.         }
  48.         /*
  49.         elseif ($this->requestStack->getMainRequest() !== null) {
  50.             $host = explode('.', $this->requestStack->getCurrentRequest()->server->get('HTTP_HOST'));
  51.             $subdomain = $host[0];
  52.         }*/
  53.         else {
  54.             $subdomain '';
  55.         }
  56.         return $subdomain;
  57.     }
  58.     public function getDefaultAdvertiser(): ?Advertiser
  59.     {
  60.         if (!$this->defaultAdvertiser instanceof Advertiser) {
  61.             $this->defaultAdvertiser $this->advertiserRepository->findOneBy([
  62.                 'subdomain' => $this->staciSubDomain,
  63.             ]);
  64.         }
  65.         return $this->defaultAdvertiser;
  66.     }
  67.     public function getDefaultSubdomain(): ?string
  68.     {
  69.         $defaultAdvertiser $this->getDefaultAdvertiser();
  70.         return $defaultAdvertiser->getSubdomain();
  71.     }
  72.     public function getCurrentAdvertiser(): ?Advertiser
  73.     {
  74.         $advertiser null;
  75.         if ($this->requestStack->getMainRequest() !== null && $this->requestStack->getMainRequest()->hasSession() && null !== $this->requestStack->getMainRequest()->getSession()->get('advertiser')) {
  76.             $advertiserSession $this->requestStack->getSession()->get('advertiser');
  77.             return $this->entityManager->getRepository(Advertiser::class)
  78.                 ->find($advertiserSession);
  79.         }
  80.         if (!$this->currentAdvertiser instanceof Advertiser) {
  81.             $this->currentAdvertiser $this->advertiserRepository->findOneBy([
  82.                 'subdomain' => $this->getSubdomain(),
  83.             ]);
  84.             $advertiser $this->currentAdvertiser;
  85.         } else {
  86.             $advertiser $this->currentAdvertiser;
  87.         }
  88.         return $advertiser;
  89.     }
  90.     public function getCurrentAdvertiserByDomain(): ?Advertiser
  91.     {
  92.         return $this->advertiserRepository->findOneBy([
  93.             'subdomain' => $this->getSubdomain(),
  94.         ]);
  95.     }
  96.     public function getAdvertisers(): array
  97.     {
  98.         return $this->advertiserRepository->findBy(['status' => true], ['title' => 'ASC']);
  99.     }
  100.     public function isDefaultAdvertiser(Advertiser $advertiser): bool
  101.     {
  102.         $defaultAdvertiser $this->getDefaultAdvertiser();
  103.         if ($defaultAdvertiser->getId() === $advertiser->getId()) {
  104.             return true;
  105.         }
  106.         return false;
  107.     }
  108.     public function getUserFilterListParams(Advertiser $advertiserRequest $request): array
  109.     {
  110.         $params = [
  111.             'email'        => '',
  112.             'role'         => '',
  113.             'businessUnit' => '',
  114.             'region'       => '',
  115.             'username'     => '',
  116.             'lastname'     => '',
  117.             'firstname'    => '',
  118.         ];
  119.         $userFilterList $request->query->get('user_filter_list');
  120.         foreach ($params as $key => $value) {
  121.             if (\is_array($userFilterList) && null !== $userFilterList[$key] && '' !== $userFilterList[$key]) {
  122.                 $params[$key] = $userFilterList[$key];
  123.                 // $exportPath .= '&email='.$request->query->get('user_filter_list')['email'];
  124.             }
  125.         }
  126.         return $params;
  127.     }
  128.     public function getElfinderInstance(): string
  129.     {
  130.         $config 'default';
  131.         $advertiser $this->getCurrentAdvertiser();
  132.         $ref $advertiser->getRef();
  133.         $filePath sprintf('%s/config/packages/fm_elfinder.yaml'__DIR__ '/../..');
  134.         $fileContent Yaml::parseFile($filePath);
  135.         if (isset($fileContent['fm_elfinder']['instances'][$ref])) {
  136.             $config $ref;
  137.         }
  138.         return $config;
  139.     }
  140.     /**
  141.      * @param string $type
  142.      */
  143.     public function getCkeditorInstance($type): string
  144.     {
  145.         $configName 'default_' $type '_config';
  146.         $advertiser $this->getCurrentAdvertiser();
  147.         $refConfig $advertiser->getRef() . '_' $type '_config';
  148.         $filePath sprintf('%s/config/packages/fos_ckeditor.yaml'__DIR__ '/../..');
  149.         $fileContent Yaml::parseFile($filePath);
  150.         if (isset($fileContent['fos_ck_editor']['configs'][$refConfig])) {
  151.             $configName $refConfig;
  152.         }
  153.         return $configName;
  154.     }
  155. }