src/CmsBundle/Controller/ContentController.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\CmsBundle\Controller;
  3. use App\CasinoBundle\Service\Bonus\BonusHtmlExtractorService;
  4. use App\CmsBundle\Exception\PageNotFoundException;
  5. use App\CmsBundle\Exception\RedirectException;
  6. use App\CmsBundle\Exception\SiteNotFoundException;
  7. use App\CmsBundle\Helper\ContentHelper;
  8. use App\CmsBundle\Service\CmsService;
  9. use App\CmsBundle\Service\MetricsCollectorService;
  10. use App\CmsBundle\Service\OptionResolverService;
  11. use Prometheus\Exception\MetricsRegistrationException;
  12. use Psr\Cache\InvalidArgumentException;
  13. use Psr\Container\ContainerExceptionInterface;
  14. use Psr\Container\NotFoundExceptionInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SymfonyAbstractController;
  16. use Symfony\Component\Cache\Adapter\AdapterInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Symfony\Component\Stopwatch\Stopwatch;
  20. final class ContentController extends SymfonyAbstractController
  21. {
  22.     private const EXCEPTION_MESSAGE_PAGE_NOT_FOUND 'Page does not exist';
  23.     private const EXCEPTION_MESSAGE_SITE_NOT_FOUND 'Site does not exist';
  24.     private const BLOCKED_DOMAINS = [
  25.         'kasinotanalyzer.com',
  26.         'spanishcasinosanalyzer.com',
  27.         'arabcasinosanalyzer.com',
  28.         'frenchcasinosanalyzer.com',
  29.         'irishcasinosanalyzer.com',
  30.         'thaicasinosanalyzer.com',
  31.         'swisscasinosanalyzer.com',
  32.         'norskcasinosanalyzer.com',
  33.         'kaszinokanalyzer.com',
  34.         'kazinoanalyzer.com',
  35.         'kasinaanalyzer.com',
  36.         'casinoernalyzer.com',
  37.         'greekcasinosanalyzer.com',
  38.         'casinolaranalyzer.com',
  39.         'vietnamcasinosanalyzer.com',
  40.         'chinacasinosanalyzer.com',
  41.         'ukrcasinosanalyzer.com',
  42.         'slvcasinosanalyzer.com',
  43.         'argcasinosanalyzer.com',
  44.         'blrcasinosanalyzer.com',
  45.         'mdacasinosanalyzer.com',
  46.         'tjkcasinosanalyzer.com',
  47.         'tkmcasinosanalyzer.com',
  48.         'serbiancasinosanalyzer.com',
  49.         'bulgariancasinosanalyzer.com',
  50.         'casinosanalyzer.jp',
  51.         'kasinaanalyzer.sk',
  52.         'casinosanalyzer.kr',
  53.         'casinosanalyzer.it',
  54.         'cassinosanalyzer.com.br',
  55.         'casinosanalyzer.nl',
  56.         'cazinourianalyzer.ro',
  57.         'casinosanalyzer.ug',
  58.         'casinosanalyzer.si',
  59.         'casinosanalyzer.co.ke',
  60.         'casinosanalyzer.co.tz',
  61.         'casinosanalyzer.co.za',
  62.         'casinosanalyzer.at',
  63.     ];
  64.     private CmsService $cmsService;
  65.     private OptionResolverService $optionResolverService;
  66.     private MetricsCollectorService $metricsCollectorService;
  67.     protected AdapterInterface $cache;
  68.     protected BonusHtmlExtractorService $bonusHtmlExtractorService;
  69.     public function __construct(
  70.         CmsService                $cmsService,
  71.         OptionResolverService     $optionResolverService,
  72.         MetricsCollectorService   $metricsCollectorService,
  73.         AdapterInterface          $cache,
  74.         BonusHtmlExtractorService $bonusHtmlExtractorService
  75.     )
  76.     {
  77.         $this->cmsService $cmsService;
  78.         $this->optionResolverService $optionResolverService;
  79.         $this->metricsCollectorService $metricsCollectorService;
  80.         $this->cache $cache;
  81.         $this->bonusHtmlExtractorService $bonusHtmlExtractorService;
  82.     }
  83.     /**
  84.      * @return Response
  85.      * @throws ContainerExceptionInterface
  86.      * @throws NotFoundExceptionInterface
  87.      * @throws MetricsRegistrationException
  88.      * @throws InvalidArgumentException
  89.      * @throw NotFoundHttpException
  90.      * @throw RedirectException
  91.      */
  92.     public function index(): Response
  93.     {
  94.         $this->optionResolverService->getRequest()?->attributes->set('is_main_request'true);
  95.         if (in_array($this->cmsService->removeSubdomainPrefix($_SERVER['HTTP_HOST']), self::BLOCKED_DOMAINS)) {
  96.             return new Response(
  97.                 '',
  98.                 Response::HTTP_FORBIDDEN,
  99.             );
  100.         }
  101.         try {
  102.             $content $this->getContentFromCache();
  103.             /*
  104.              * Need Check Page Loading [5563]
  105.              *
  106.             $offersData = $this->bonusHtmlExtractorService->extractFromHtml(
  107.                 $content,
  108.                 $this->optionResolverService->getRequest()
  109.             );
  110.             $this->optionResolverService
  111.                 ->getRequest()
  112.                 ->attributes
  113.                 ->set(
  114.                     'bonus_offer_stats',
  115.                     $offersData
  116.                 );
  117.             */
  118.             return new Response(
  119.                 $content,
  120.                 Response::HTTP_OK,
  121.                 $this->getHeaders()
  122.             );
  123.         } catch (PageNotFoundException $e) {
  124.             throw new NotFoundHttpException(
  125.                 sprintf('%s: %s'self::EXCEPTION_MESSAGE_PAGE_NOT_FOUND$this->optionResolverService->getRequest()?->getUri()),
  126.                 $e
  127.             );
  128.         } catch (SiteNotFoundException $e) {
  129.             throw new NotFoundHttpException(
  130.                 sprintf('%s: %s'self::EXCEPTION_MESSAGE_SITE_NOT_FOUND$this->optionResolverService->getRequest()?->getUri()),
  131.                 $e
  132.             );
  133.         } catch (RedirectException $exception) {
  134.             if (!empty($exception->getUrl())) {
  135.                 return $this->redirect($exception->getUrl(), $exception->getStatus());
  136.             } else {
  137.                 throw new NotFoundHttpException(self::EXCEPTION_MESSAGE_PAGE_NOT_FOUND);
  138.             }
  139.         }
  140.     }
  141.     /**
  142.      * @return string|null
  143.      * @throws ContainerExceptionInterface
  144.      * @throws NotFoundExceptionInterface
  145.      * @throws PageNotFoundException
  146.      * @throws RedirectException
  147.      * @throws SiteNotFoundException
  148.      * @throws MetricsRegistrationException
  149.      */
  150.     private function getContent(): ?string
  151.     {
  152.         $stopwatch = new Stopwatch();
  153.         $stopwatch->start('response_generation_time');
  154.         $stopwatch->start('content_generation_time');
  155.         $domain $this->optionResolverService->getSite()->getDomain();
  156.         $environment $this->optionResolverService->getEnvironment();
  157.         $route $this->optionResolverService->getRoute()?->getParameter('route');
  158.         $pageData $this->cmsService->getPageData();
  159.         $this->metricsCollectorService->incContentGenerationTime(
  160.             $stopwatch->stop('content_generation_time')->getDuration(),
  161.             $environment,
  162.             $domain,
  163.             $route
  164.         );
  165.         $content $this->render(
  166.             $this->cmsService->getPageTemplate(),
  167.             $pageData
  168.         )->getContent();
  169.         $this->metricsCollectorService->incResponseGenerationTime(
  170.             $stopwatch->stop('response_generation_time')->getDuration(),
  171.             $environment,
  172.             $domain,
  173.             $route
  174.         );
  175.         return ContentHelper::removeShortCodes($content);
  176.     }
  177.     /**
  178.      * @return string
  179.      * @throws ContainerExceptionInterface
  180.      * @throws NotFoundExceptionInterface
  181.      * @throws PageNotFoundException
  182.      * @throws RedirectException
  183.      * @throws SiteNotFoundException
  184.      * @throws MetricsRegistrationException
  185.      * @throws InvalidArgumentException
  186.      */
  187.     private function getContentFromCache(): string
  188.     {
  189.         $applyCache = (!$this->getUser() and $this->optionResolverService->getEnvironment() != 'loc');
  190.         $cachedResult null;
  191.         if ($applyCache) {
  192.             $cacheKey 'page_' md5($this->optionResolverService->getClientCountryCodeCf() . '_' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  193.             $cachedResult $this->cache->getItem($cacheKey);
  194.         }
  195.         if (($cachedResult or $cachedResult?->isHit()) and ($content $cachedResult->get())) {
  196.             //release content from cache
  197.         } else {
  198.             $content $this->getContent();
  199.             if ($applyCache) {
  200.                 $cachedResult->set($content);
  201.                 $cachedResult->expiresAfter(172800); // 2d
  202.                 $this->cache->save($cachedResult);
  203.             }
  204.         }
  205.         return $content;
  206.     }
  207.     /**
  208.      * @return string[]
  209.      */
  210.     private function getHeaders(): array
  211.     {
  212.         return [
  213.             'X-Frame-Options'             => 'SAMEORIGIN',
  214.             'Access-Control-Allow-Origin' => '*',
  215.             'Link'                        => '<https://api.amplitude.com>; rel="preconnect"'
  216.                 ', <https://cdn.amplitude.com>; rel="preconnect"'
  217.                 ', <https://cdn.growthbook.io>; rel="preconnect"'
  218.                 ', </build/midori/assets/bootstrap/css/bootstrap-grid.min.css>; rel="preload"; as="style"; crossorigin="anonymous"'
  219.                 ', </build/midori/assets/css/base.css?v=' $this->optionResolverService->getAppVersion() . '>; rel="preload"; as="style"; crossorigin="anonymous"'
  220.                 ', </build/midori/assets/fonts/Poppins-Bold.woff2>; rel="preload"; as="font"; crossorigin="anonymous"'
  221.                 ', </build/midori/assets/fonts/Poppins-SemiBold.woff2>; rel="preload"; as="font"; crossorigin="anonymous"'
  222.                 ', </build/midori/assets/fonts/Poppins-Medium.woff2>; rel="preload"; as="font"; crossorigin="anonymous"'
  223.                 ', </build/midori/assets/fonts/Poppins-Regular.woff2>; rel="preload"; as="font"; crossorigin="anonymous"'
  224.                 ', </build/midori/assets/fonts/Poppins-ExtraBold.woff2>; rel="preload"; as="font"; crossorigin="anonymous"'
  225.         ];
  226.     }
  227. }