src/Services/MeOnMap/GetCategoriesWithGeoLocationsResponseCacheService.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\Services\MeOnMap;
  4. use Slivki\Dao\Category\CategoryDaoInterface;
  5. use Slivki\Dao\MeOnMap\OfferPositionPaidDaoInterface;
  6. use Slivki\Entity\Category;
  7. use Slivki\Entity\MainMenu;
  8. use Slivki\Factory\Category\CategoryWithGeoLocationsDtoFactoryInterface;
  9. use Slivki\Factory\MeOnMap\OfferFeaturedPaidFactoryInterface;
  10. use Slivki\Repository\Category\CategoryRepositoryInterface;
  11. use Slivki\Repository\MainMenu\MainMenuRepositoryInterface;
  12. use Slivki\Response\MeOnMap\GetCategoriesWithGeoLocationsResponse;
  13. use Slivki\Serializer\Config\SerializerConfig;
  14. use Slivki\Util\SoftCache;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. use function sprintf;
  17. use function array_map;
  18. use function count;
  19. final class GetCategoriesWithGeoLocationsResponseCacheService
  20. {
  21.     private const CACHE_NAME 'me-on-map';
  22.     private const CACHE_KEY 'get-categories-with-geo-location-response-city-%s';
  23.     private const CACHE_TIME_SECONDS 3600;
  24.     private MainMenuRepositoryInterface $mainMenuRepository;
  25.     private CategoryRepositoryInterface $categoryRepository;
  26.     private CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory;
  27.     private CategoryDaoInterface $categoryDao;
  28.     private OfferPositionPaidDaoInterface $offerPositionPaidDao;
  29.     private OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory;
  30.     private SerializerInterface $serializer;
  31.     public function __construct(
  32.         MainMenuRepositoryInterface $mainMenuRepository,
  33.         CategoryRepositoryInterface $categoryRepository,
  34.         CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory,
  35.         CategoryDaoInterface $categoryDao,
  36.         OfferPositionPaidDaoInterface $offerPositionPaidDao,
  37.         OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory,
  38.         SerializerInterface $serializer
  39.     ) {
  40.         $this->mainMenuRepository $mainMenuRepository;
  41.         $this->categoryRepository $categoryRepository;
  42.         $this->categoryWithGeoLocationsDtoFactory $categoryWithGeoLocationsDtoFactory;
  43.         $this->categoryDao $categoryDao;
  44.         $this->offerPositionPaidDao $offerPositionPaidDao;
  45.         $this->offerFeaturedPaidFactory $offerFeaturedPaidFactory;
  46.         $this->serializer $serializer;
  47.     }
  48.     public function getByCityId(int $cityId): GetCategoriesWithGeoLocationsResponse
  49.     {
  50.         $cache = new SoftCache(self::CACHE_NAME);
  51.         $json $cache->get($this->getCacheKeyByCityId($cityId));
  52.         if (empty($json)) {
  53.             $this->reloadByCityId($cityId);
  54.             $json $cache->get($this->getCacheKeyByCityId($cityId));
  55.         }
  56.         return $this->serializer->deserialize($jsonGetCategoriesWithGeoLocationsResponse::class, SerializerConfig::JSON);
  57.     }
  58.     public function reloadByCityId(int $cityId): void
  59.     {
  60.         $cache = new SoftCache(self::CACHE_NAME);
  61.         $activeMeOnMapCategories $this->categoryRepository->findActiveMeOnMapCategoriesByCityId($cityId);
  62.         if (=== count($activeMeOnMapCategories)) {
  63.             $activeMeOnMapCategories array_map(
  64.                 fn (MainMenu $mainMenuItem): Category =>
  65.                 $this->categoryRepository->findCategoryByMainMenu($mainMenuItem),
  66.                 $this->mainMenuRepository->findWithActiveOfferCategoriesByCityId($cityId),
  67.             );
  68.         }
  69.         $activeMeOnMapFeaturedCategories $this->categoryRepository->findActiveMeOnMapFeaturedCategoriesByCityId($cityId);
  70.         if (=== count($activeMeOnMapFeaturedCategories)) {
  71.             $featuredOnMapFilterCategoriesId $this->categoryDao->getFeaturedOnMapFilterCategoriesId($cityId);
  72.             $activeMeOnMapFeaturedCategories $this->categoryRepository->findByIds($featuredOnMapFilterCategoriesId);
  73.         } else {
  74.             $featuredOnMapFilterCategoriesId array_map(
  75.                 static fn(Category $category): int => $category->getID(),
  76.                 $activeMeOnMapFeaturedCategories,
  77.             );
  78.         }
  79.         $response = new GetCategoriesWithGeoLocationsResponse(
  80.             array_map([$this->categoryWithGeoLocationsDtoFactory'create'], $activeMeOnMapCategories),
  81.             array_map([$this->categoryWithGeoLocationsDtoFactory'create'], $activeMeOnMapFeaturedCategories),
  82.             $featuredOnMapFilterCategoriesId,
  83.             array_map([$this->offerFeaturedPaidFactory'create'], $this->offerPositionPaidDao->findAllByCityId($cityId)),
  84.         );
  85.         $cache->set(
  86.             $this->getCacheKeyByCityId($cityId),
  87.             $this->serializer->serialize($responseSerializerConfig::JSON),
  88.             self::CACHE_TIME_SECONDS
  89.         );
  90.     }
  91.     private function getCacheKeyByCityId(int $cityId): string
  92.     {
  93.         return sprintf(self::CACHE_KEY$cityId);
  94.     }
  95. }