src/Services/BannerService.php line 269

Open in your IDE?
  1. <?php
  2. namespace Slivki\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Slivki\Entity\Banner\AbstractMobileAppBanner;
  5. use Slivki\Entity\Banner\CategoryMobileAppBanner;
  6. use Slivki\Entity\Banner\ExternalMobileAppBanner;
  7. use Slivki\Entity\Banner\OfferMobileAppBanner;
  8. use Slivki\Entity\City;
  9. use Slivki\Entity\Banner;
  10. use Slivki\Entity\Category;
  11. use Slivki\Entity\SidebarBanner;
  12. use Slivki\Repository\Banner\CommentsBannerRepositoryInterface;
  13. use Slivki\Repository\Banner\MobileAppBannerRepositoryInterface;
  14. use Slivki\Util\SoftCache;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Session\Session;
  17. use Symfony\Component\HttpKernel\KernelInterface;
  18. final class BannerService
  19. {
  20.     const CACHE_NAME 'banner-1-';
  21.     const MOBILE_CACHE_NAME_PREFIX 'mobile-1-';
  22.     const CACHE_INDEX_KEY 'index-for-comments-3-';
  23.     const SIDEBAR_BANNER_CACHE_KEY 'sidebar-banner-12-1-';
  24.     const MAIN_MENU_BANNER_CACHE_KEY 'main-menu-banner-1-';
  25.     const CACHE_VERSION_CACHE_KEY '-version-1-';
  26.     const MOBILE_APP_MAIN_PAGE_BANNER_CACHE_KEY 'mobile-app-1-';
  27.     // offerId => bannerId
  28.     private const BANNER_BAN_LIST = [
  29.         143636 => 9322
  30.     ];
  31.     private $bannerRepository;
  32.     private $entityManager;
  33.     private $container;
  34.     private $kernel;
  35.     private MobileAppBannerRepositoryInterface $mobileAppBannerRepository;
  36.     private CommentsBannerRepositoryInterface $commentsBannerRepository;
  37.     public function __construct(
  38.         EntityManagerInterface $entityManager,
  39.         ContainerInterface $container,
  40.         KernelInterface $kernel,
  41.         MobileAppBannerRepositoryInterface $mobileAppBannerRepository,
  42.         CommentsBannerRepositoryInterface $commentsBannerRepository
  43.     ) {
  44.         $this->bannerRepository $entityManager->getRepository(Banner::class);
  45.         $this->entityManager $entityManager;
  46.         $this->container $container;
  47.         $this->kernel $kernel;
  48.         $this->mobileAppBannerRepository $mobileAppBannerRepository;
  49.         $this->commentsBannerRepository $commentsBannerRepository;
  50.     }
  51.     public function getHeadBannerCached($cityID$categoryIDs$mobile$mobileCache false) {
  52.         $cacheName $mobileCache Banner::MOBILE_HEAD_BANNER_CACHE_NAME Banner::HEAD_BANNER_CACHE_NAME;
  53.         $cacheName .= $this->getCacheVersion() . '-' $cityID;
  54.         $softCache = new SoftCache($cacheName);
  55.         $bannersAmount $softCache->get('amount');
  56.         if (!$bannersAmount) {
  57.             $this->reloadHeadBannerCache($cityID$mobileCache);
  58.             $bannersAmount $softCache->get('amount');
  59.         }
  60.         if ($bannersAmount <= || $bannersAmount == SoftCache::LOCKED_KEY) {
  61.             return '';
  62.         }
  63.         $cacheKey $mobile '-mobile' '';
  64.         $bannerHtml '';
  65.         $banners = [];
  66.         for ($i 1$i <= $bannersAmount$i++) {
  67.             $banner $softCache->get($i $cacheKey);
  68.             $banners[$i] = $banner;
  69.         }
  70.         $session = new Session();
  71.         $bannerIndexSessionKey $mobile '2-bannerIndex-mobile' '2-bannerIndex';
  72.         $bannerIndex $session->get($bannerIndexSessionKey);
  73.         if (!$bannerIndex || $bannerIndex $bannersAmount) {
  74.             $bannerIndex 1;
  75.             $session->set($bannerIndexSessionKey$bannerIndex);
  76.         }
  77.         $categoryBannerIDs = [];
  78.         $isPassThroughOnly true;
  79.         foreach ($categoryIDs as $categoryID) {
  80.             $currentCategory $this->entityManager->getRepository(Category::class)->findCached((int)$categoryID);
  81.             if (isset($currentCategory['category'])) {
  82.                 foreach ($banners as $key => $banner) {
  83.                     if (in_array($key$categoryBannerIDs) || (!$banner['categoryID'] && !$banner['passThrough'])) {
  84.                         continue;
  85.                     }
  86.                     if ($banner['passThrough']) {
  87.                         $categoryBannerIDs[] = $key;
  88.                     } elseif ($banner['categoryID'] == $categoryID || $currentCategory['category']->isChildOfRecursive($banner['categoryID']) || $banner['passThrough']) {
  89.                         $categoryBannerIDs[] = $key;
  90.                         if (!$banner['passThrough']) {
  91.                             $isPassThroughOnly false;
  92.                         }
  93.                     }
  94.                 }
  95.             }
  96.         }
  97.         if (empty($categoryBannerIDs) || $isPassThroughOnly) {
  98.             foreach ($banners as $key => $banner) {
  99.                 if ($banner['categoryID'] && !$banner['passThrough']) {
  100.                     unset($banners[$key]);
  101.                 }
  102.             }
  103.             if (count($banners) == 0) {
  104.                 return '';
  105.             }
  106.             $banners array_combine(range(1count($banners)), array_values($banners));
  107.             $bannersAmount count($banners);
  108.             if ($bannerIndex $bannersAmount) {
  109.                 $bannerIndex 1;
  110.                 $session->set($bannerIndexSessionKey$bannerIndex);
  111.             }
  112.             if (isset($banners[$bannerIndex]['html']) && (!$banners[$bannerIndex]['categoryID'] || $banners[$bannerIndex]['passThrough'])) {
  113.                 $bannerHtml $banners[$bannerIndex]['html'];
  114.                 $session->set($bannerIndexSessionKey$bannerIndex 1);
  115.             } else {
  116.                 $nextBannerIndex $bannerIndex 1;
  117.                 $nextBannerIndex $nextBannerIndex $bannersAmount $nextBannerIndex;
  118.                 while ($nextBannerIndex != $bannerIndex) {
  119.                     if (isset($banners[$nextBannerIndex]['html'])) {
  120.                         $bannerHtml $banners[$nextBannerIndex]['html'];
  121.                         $session->set($bannerIndexSessionKey$nextBannerIndex);
  122.                         break;
  123.                     }
  124.                     $nextBannerIndex++;
  125.                     $nextBannerIndex $nextBannerIndex $bannersAmount $nextBannerIndex;
  126.                 }
  127.             }
  128.         } else {
  129.             if (isset($banners[$bannerIndex]['html']) && in_array($bannerIndex$categoryBannerIDs)) {
  130.                 $bannerHtml $banners[$bannerIndex]['html'];
  131.                 $session->set($bannerIndexSessionKey$bannerIndex 1);
  132.             } else {
  133.                 $nextBannerIndex $bannerIndex 1;
  134.                 $nextBannerIndex $nextBannerIndex $bannersAmount $nextBannerIndex;
  135.                 while ($nextBannerIndex != $bannerIndex) {
  136.                     if (isset($banners[$nextBannerIndex]['html']) && in_array($nextBannerIndex$categoryBannerIDs)) {
  137.                         $bannerHtml $banners[$nextBannerIndex]['html'];
  138.                         $session->set($bannerIndexSessionKey$nextBannerIndex 1);
  139.                         break;
  140.                     }
  141.                     $nextBannerIndex++;
  142.                     $nextBannerIndex $nextBannerIndex $bannersAmount $nextBannerIndex;
  143.                 }
  144.             }
  145.         }
  146.         return $bannerHtml $bannerHtml '';
  147.     }
  148.     public function resetHeadBannerCache() {
  149.         $softCache = new SoftCache(self::CACHE_NAME);
  150.         $version $softCache->get(self::CACHE_VERSION_CACHE_KEY0);
  151.         $version++;
  152.         $softCache->set(self::CACHE_VERSION_CACHE_KEY$version,30 60);
  153.     }
  154.     public function reloadHeadBannerCache($cityID$mobileCache false) {
  155.         $cacheName $mobileCache Banner::MOBILE_HEAD_BANNER_CACHE_NAME Banner::HEAD_BANNER_CACHE_NAME;
  156.         $cacheName .= $this->getCacheVersion() . '-' $cityID;
  157.         $softCache = new SoftCache($cacheName);
  158.         $previousBannersAmount $softCache->get('amount');
  159.         if ($previousBannersAmount == SoftCache::LOCKED_KEY) {
  160.             return;
  161.         }
  162.         $softCache->set('amount'SoftCache::LOCKED_KEY30);
  163.         if (!$previousBannersAmount) {
  164.             $previousBannersAmount 0;
  165.         }
  166.         $bannerList $this->entityManager->getRepository(Banner\HeaderBanner::class)->findBy(['active' => true], ['ID' => 'DESC']);
  167.         $currentBannerAmount 0;
  168.         $bannerListByCity = [];
  169.         foreach ($bannerList as $key => $banner) {
  170.             if (in_array($cityID$banner->getCityIds(), true)) {
  171.                 $bannerListByCity[$key] = $banner;
  172.             }
  173.         }
  174.         if (!empty($bannerListByCity)) {
  175.             $bannerList $bannerListByCity;
  176.         }
  177.         /** @var Banner\HeaderBanner $banner */
  178.         foreach ($bannerList as $key => $banner) {
  179.             if (!$mobileCache && $banner->getCodeFilePath()) {
  180.                 continue;
  181.             }
  182.             if ($banner->getCityIds() && !in_array($cityID$banner->getCityIds(), true)) {
  183.                 continue;
  184.             }
  185.             $currentBannerAmount++;
  186.             if ($banner->isJavaScript()) {
  187.                 $bannerHTML $banner->getCode();
  188.                 if ($banner->getCodeFilePath()) {
  189.                     $bannerHTML = @file_get_contents(realpath($this->kernel->getProjectDir() . '/public') . $banner->getCodeFilePath());
  190.                     $softCache->set($currentBannerAmount '-mobile', ['categoryID' => $banner->getCategoryID(), 'passThrough' => $banner->isPassThrough(), 'html' => $bannerHTML], 0);
  191.                 }
  192.             } else {
  193.                 if (stripos($banner->getFilePath(), '.swf') !== false) {
  194.                     $data = [
  195.                         'banner' => $banner,
  196.                         'width' => '100%',
  197.                         'height' => 90
  198.                     ];
  199.                     $bannerHTML $this->container->get('twig')->render('Slivki/banners/swf_banner.html.twig'$data);
  200.                 } else {
  201.                     $view 'Slivki/banners/head_banner.html.twig';
  202.                     if ($mobileCache) {
  203.                         $view 'Slivki/mobile/banner/header_banner.html.twig';
  204.                     }
  205.                     $bannerHTML $this->container->get('twig')->render($view, ['topSiteBanner' => $banner]);
  206.                 }
  207.             }
  208.             $softCache->set($currentBannerAmount, ['categoryID' => $banner->getCategoryID(), 'passThrough' => $banner->isPassThrough(), 'html' => $bannerHTML], 0);
  209.             if ($banner->getFilePathMobile() != '') {
  210.                 if (!$banner->getCodeFilePath()) {
  211.                     $view 'Slivki/banners/head_banner.html.twig';
  212.                     if ($mobileCache) {
  213.                         $view 'Slivki/mobile/banner/header_banner.html.twig';
  214.                     }
  215.                     $banner->setFilePath($banner->getFilePathMobile());
  216.                     $bannerHTML $this->container->get('twig')->render($view, ['topSiteBanner' => $banner]);
  217.                 }
  218.                 $softCache->set($currentBannerAmount '-mobile', ['categoryID' => $banner->getCategoryID(), 'passThrough' => $banner->isPassThrough(), 'html' => $bannerHTML], 0);
  219.             }
  220.         };
  221.         for ($i $currentBannerAmount 1$i <= $previousBannersAmount$i++) {
  222.             $softCache->delete($i);
  223.         }
  224.         $softCache->set('amount'$currentBannerAmount30 60);
  225.     }
  226.     private function getCacheVersion() {
  227.         return (new SoftCache(self::CACHE_NAME))->get(self::CACHE_VERSION_CACHE_KEY0);
  228.     }
  229.     public function getMainMenuBannerCached() {
  230.         $softCache = new SoftCache(self::CACHE_NAME);
  231.         $cacheKey self::MAIN_MENU_BANNER_CACHE_KEY;
  232.         $mainMenuBannerList $softCache->get($cacheKey, []);
  233.         return $mainMenuBannerList;
  234.     }
  235.     public function reloadMainMenuBannerCache() {
  236.         $softCache = new SoftCache(self::CACHE_NAME);
  237.         $cacheKey self::MAIN_MENU_BANNER_CACHE_KEY;
  238.         $mainMenuBannerList $this->entityManager->getRepository(Banner\MainMenuBanner::class)->findBy(['active' => true], ['ID' => 'DESC']);
  239.         $twig $this->container->get('twig');
  240.         $i 0;
  241.         $banners = [];
  242.         foreach ($mainMenuBannerList as $banner) {
  243.             $banners[$i] = $twig->render('Slivki/banners/main_menu_banner.html.twig', ['banner' => $banner'index' => $i]);
  244.             $i++;
  245.         }
  246.         $softCache->set($cacheKey$banners0);
  247.     }
  248.     public function getCategoryBannerCached(Category $category null$isMobile) {
  249.         if (!$category) {
  250.             return '';
  251.         }
  252.         $softCache = new SoftCache(Banner::CATEGORY_BANNER_CACHE_NAME);
  253.         $cachedBannerList $softCache->get('category'.$category->getID());
  254.         if (!$cachedBannerList) {
  255.             $cachedBannerList $this->reloadCategoryBannerCache($category);
  256.         }
  257.         if ($cachedBannerList == '') {
  258.             return '';
  259.         }
  260.         $bannerIDList explode(','$cachedBannerList);
  261.         return $this->getCategoryBannerCachedByID($bannerIDList[array_rand($bannerIDList)], $isMobile);
  262.     }
  263.     public function reloadCategoryBannerCache(Category $category) {
  264.         $softCache = new SoftCache(Banner::CATEGORY_BANNER_CACHE_NAME);
  265.         $bannerList $category->getActiveBannersByType(Banner::TYPE_CATEGORY_BANNER);
  266.         $bannerIDList = [];
  267.         foreach ($bannerList as $banner) {
  268.             $bannerIDList[] = $banner->getID();
  269.         }
  270.         $imlodedBanerIDList implode($bannerIDList',');
  271.         if (empty($bannerIDList)) {
  272.             $softCache->delete('category'.$category->getID());
  273.             return '';
  274.         }
  275.         $softCache->set('category'.$category->getID(), $imlodedBanerIDList30 40);
  276.         return $imlodedBanerIDList;
  277.     }
  278.     public function reloadCategoryBannerListCache() {
  279.         $bannerList $this->bannerRepository->findBy(['typeID' => Banner::TYPE_CATEGORY_BANNER'active' => true]);
  280.         foreach ($bannerList as $banner) {
  281.             $this->reloadCategoryBannerCacheByID($banner->getID());
  282.         }
  283.     }
  284.     public function getCategoryBannerCachedByID($bannerID$isMobile false) {
  285.         $softCache = new SoftCache($isMobile self::MOBILE_CACHE_NAME_PREFIX Banner::CATEGORY_BANNER_CACHE_NAME
  286.             Banner::CATEGORY_BANNER_CACHE_NAME);
  287.         $bannerContent $softCache->get($bannerID);
  288.         if (!$bannerContent) {
  289.             $bannerContent $this->reloadCategoryBannerCacheByID($bannerID$isMobile);
  290.         }
  291.         return $bannerContent;
  292.     }
  293.     public function reloadCategoryBannerCacheByID($bannerID$isMobile false) {
  294.         $softCache = new SoftCache(Banner::CATEGORY_BANNER_CACHE_NAME);
  295.         $mobileSoftCache = new SoftCache(self::MOBILE_CACHE_NAME_PREFIX Banner::CATEGORY_BANNER_CACHE_NAME);
  296.         /** @var Banner $banner */
  297.         $banner $this->bannerRepository->find($bannerID);
  298.         if (!$banner) {
  299.             return '';
  300.         }
  301.         if ($banner->getTypeID() != Banner::TYPE_CATEGORY_BANNER) {
  302.             return '';
  303.         }
  304.         if (!$banner->isActive()) {
  305.             $softCache->delete($bannerID);
  306.         }
  307.         $data['categoryBanner'] = $banner;
  308.         $twig $this->container->get('twig');
  309.         $bannerContent $twig->render('Slivki/banners/category_banner.html.twig'$data);
  310.         $softCache->set($bannerID$bannerContent60 40);
  311.         $mobileBannerContent $twig->render('Slivki/mobile/banner/banner.html.twig', ['banner' => $banner]);
  312.         $mobileSoftCache->set($bannerID$mobileBannerContent60 40);
  313.         return $isMobile $mobileBannerContent $bannerContent;
  314.     }
  315.     public function getCommentsBanners($mobileDevice false$cityID City::DEFAULT_CITY_ID$offerId null) {
  316.         $commentsBannerList $this->commentsBannerRepository->findActiveByCities([$cityID]);
  317.         $commentsBannerHtmlList = [];
  318.         $view $mobileDevice 'Slivki/mobile/banner/banner.html.twig' 'Slivki/banners/comments_banner.html.twig';
  319.         /** @var Banner $banner */
  320.         foreach($commentsBannerList as $banner) {
  321.             if (null !== $offerId && !empty(self::BANNER_BAN_LIST[$offerId]) && (int) $banner->getID() === self::BANNER_BAN_LIST[$offerId]) {
  322.                 continue;
  323.             }
  324.             $commentsBannerHtmlList[$banner->getPositionRow()][] = $this->container->get('twig')->render($view, ['banner' => $banner]);
  325.         }
  326.         // Add offer button
  327.         ksort($commentsBannerHtmlList);
  328.         $i 1;
  329.         foreach ($commentsBannerHtmlList as $key=>$item) {
  330.             if ($i == && !isset($commentsBannerHtmlList[$key+2]) && !$mobileDevice) {
  331.                 $commentsBannerHtmlList[$key+2][0] = '<div class="add-offer-comment-link"><a class="button--rose button" href="/moya_akciya" onclick="ga(\'send\', \'event\', \'create-own-offer-button--comments\', \'Click\', \'1\');">Разместить свою акцию на Slivki.by</a></div>';
  332.             }
  333.             if ($i 2) {
  334.                 break;
  335.             }
  336.             $i++;
  337.         }
  338.         return $commentsBannerHtmlList;
  339.     }
  340.     public function getCacheIndex() {
  341.         $softCache = new SoftCache(self::CACHE_NAME);
  342.         $cacheKey self::CACHE_INDEX_KEY;
  343.         $cacheIndex $softCache->get($cacheKey);
  344.         return $cacheIndex $cacheIndex 0;
  345.     }
  346.     public function incrementCacheIndex() {
  347.         $softCache = new SoftCache(self::CACHE_NAME);
  348.         $cacheKey self::CACHE_INDEX_KEY;
  349.         $cacheIndex $softCache->get($cacheKey);
  350.         $cacheIndex $cacheIndex $cacheIndex 1;
  351.         $softCache->set($cacheKey$cacheIndex0);
  352.     }
  353.     public function getSidebarBannerCached($cityID) {
  354.         $softCache = new SoftCache(self::CACHE_NAME);
  355.         $cacheKey self::SIDEBAR_BANNER_CACHE_KEY '-' $cityID;
  356.         $cacheKeyLocked $cacheKey '-locked';
  357.         $sidebarBannerCached $softCache->get($cacheKey);
  358.         if (!$sidebarBannerCached || ($sidebarBannerCached['expiredOn'] > time() && !$softCache->get($cacheKeyLocked))) {
  359.             return $this->reloadSidebarBannerCache($cityID);
  360.         }
  361.         $sidebarBannerCached['serverTimeToBannerTime'] = strtotime(date("Y-m-d 09:00:00")) - time();
  362.         return $sidebarBannerCached;
  363.     }
  364.     public function reloadSidebarBannerCache($cityID) {
  365.         $softCache = new SoftCache(self::CACHE_NAME);
  366.         $cacheKey self::SIDEBAR_BANNER_CACHE_KEY '-' $cityID;
  367.         $cacheKeyLocked $cacheKey '-locked';
  368.         $softCache->add($cacheKeyLocked'locked'20);
  369.         $sidebarBannerRepository $this->entityManager->getRepository(SidebarBanner::class);
  370.         $bannerList $sidebarBannerRepository->findBy(['active' => true'cityID' => $cityID], ['ID' => 'ASC']);
  371.         if (!$bannerList) {
  372.             $bannerList $sidebarBannerRepository->findBy(['active' => true'cityID' => City::DEFAULT_CITY_ID], ['ID' => 'ASC']);
  373.         }
  374.         $sidebarBannerCached['serverTimeToBannerTime'] = strtotime(date("Y-m-d 09:00:00")) - time();
  375.         $sidebarBannerCached['html'] = $this->container->get('twig')->render('Slivki/banners/sidebar_banner.html.twig', [
  376.             'bannerList' => $bannerList,
  377.             'lastPurchasesOfferList' => []
  378.         ]);
  379.         $sidebarBannerCached['expiredOn'] = time() + 60 5;
  380.         $softCache->set($cacheKey$sidebarBannerCached0);
  381.         return $sidebarBannerCached;
  382.     }
  383.     public function getBeautyShopBannerCached() {
  384.         $softCache = new SoftCache(Banner::BEAUTY_SHOPS_CACHE_NAME);
  385.         $cachedBannerList $softCache->get('beautyShop');
  386.         if (!$cachedBannerList) {
  387.             $cachedBannerList $this->reloadBeautyShopBannerCache();
  388.         }
  389.         if ($cachedBannerList == '') {
  390.             return '';
  391.         }
  392.         $bannerIDList explode(','$cachedBannerList);
  393.         return $this->getBeautyShopBannerCachedByID($bannerIDList[array_rand($bannerIDList)]);
  394.     }
  395.     public function reloadBeautyShopBannerCache() {
  396.         $softCache = new SoftCache(Banner::CATEGORY_BANNER_CACHE_NAME);
  397.         $bannerList $this->bannerRepository->findBy(['typeID' => Banner::TYPE_BEAUTY_SHOPS_BANNER'active' => true]);
  398.         $bannerIDList = [];
  399.         foreach ($bannerList as $banner) {
  400.             $bannerIDList[] = $banner->getID();
  401.         }
  402.         $imlodedBanerIDList implode($bannerIDList',');
  403.         if (empty($bannerIDList)) {
  404.             $softCache->delete('beautyShop');
  405.             return '';
  406.         }
  407.         $softCache->set('beautyShop'$imlodedBanerIDList30 40);
  408.         return $imlodedBanerIDList;
  409.     }
  410.     public function reloadBeautyShopBannerListCache() {
  411.         $bannerList $this->bannerRepository->findBy(['typeID' => Banner::TYPE_BEAUTY_SHOPS_BANNER'active' => true]);
  412.         foreach ($bannerList as $banner) {
  413.             $this->reloadBeautyShopBannerCacheByID($banner->getID());
  414.         }
  415.     }
  416.     public function getBeautyShopBannerCachedByID($bannerID) {
  417.         $softCache = new SoftCache(Banner::BEAUTY_SHOPS_CACHE_NAME);
  418.         $bannerContent $softCache->get($bannerID);
  419.         if (!$bannerContent) {
  420.             $bannerContent $this->reloadBeautyShopBannerCacheByID($bannerID);
  421.         }
  422.         return $bannerContent;
  423.     }
  424.     public function reloadBeautyShopBannerCacheByID($bannerID) {
  425.         $softCache = new SoftCache(Banner::BEAUTY_SHOPS_CACHE_NAME);
  426.         /** @var Banner $banner */
  427.         $banner $this->bannerRepository->find($bannerID);
  428.         if (!$banner) {
  429.             return '';
  430.         }
  431.         if ($banner->getTypeID() != Banner::TYPE_BEAUTY_SHOPS_BANNER) {
  432.             return '';
  433.         }
  434.         if (!$banner->isActive()) {
  435.             $softCache->delete($bannerID);
  436.         }
  437.         $data['banner'] = $banner;
  438.         $bannerContent $this->container->get('twig')->render('Slivki/banners/beauty_shop_banner.html.twig'$data);
  439.         $softCache->set($bannerID$bannerContent60 40);
  440.         return $bannerContent;
  441.     }
  442.     public function getMobileAppMainPageBannerCache($cityID City::DEFAULT_CITY_ID) {
  443.         $softCache = new SoftCache(self::CACHE_NAME);
  444.         $result $softCache->get(self::MOBILE_APP_MAIN_PAGE_BANNER_CACHE_KEY $cityID);
  445.         if (!$result) {
  446.             return $this->reloadMobileAppMainPageBannerCache($cityID);
  447.         }
  448.         return $result;
  449.     }
  450.     public function reloadMobileAppMainPageBannerCache(int $cityID City::DEFAULT_CITY_ID): array
  451.     {
  452.         $softCache = new SoftCache(self::CACHE_NAME);
  453.         $result = [];
  454.         $banners $this->mobileAppBannerRepository->findActiveByCity($cityID);
  455.         $baseURL $this->container->getParameter('base_url');
  456.         /** @var AbstractMobileAppBanner $banner */
  457.         foreach ($banners as $banner) {
  458.             $entityID null;
  459.             if ($banner instanceof CategoryMobileAppBanner) {
  460.                 $entityID $banner->getCategory()->getID();
  461.                 $target 'category';
  462.                 $entityName $banner->getCategory()->getName();
  463.             } elseif ($banner instanceof OfferMobileAppBanner) {
  464.                 $entityID $banner->getOffer()->getID();
  465.                 $target 'offer';
  466.                 $entityName $banner->getOffer()->getTitle();
  467.             } elseif ($banner instanceof ExternalMobileAppBanner) {
  468.                 $target 'external';
  469.             }
  470.             $result[] = [
  471.                 'ID' => $banner->getID(),
  472.                 'imageURL' => $baseURL $banner->getFilePath(),
  473.                 'target' => $target,
  474.                 'entityID' => $entityID,
  475.                 'entityName' => $entityName,
  476.                 'externalUrl' => $banner->getURL(),
  477.             ];
  478.         }
  479.         $softCache->set(self::MOBILE_APP_MAIN_PAGE_BANNER_CACHE_KEY $cityID$result60 30);
  480.         return $result;
  481.     }
  482. }