src/MessageHandler/Query/MeOnMap/GetCategoriesWithGeoLocationsHandler.php line 47

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\MessageHandler\Query\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\Message\Query\MeOnMap\GetCategoriesWithGeoLocationsQuery;
  11. use Slivki\Repository\Category\CategoryRepositoryInterface;
  12. use Slivki\Repository\MainMenu\MainMenuRepositoryInterface;
  13. use Slivki\Response\MeOnMap\GetCategoriesWithGeoLocationsResponse;
  14. use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
  15. use function array_map;
  16. use function count;
  17. final class GetCategoriesWithGeoLocationsHandler implements MessageHandlerInterface
  18. {
  19.     private MainMenuRepositoryInterface $mainMenuRepository;
  20.     private CategoryRepositoryInterface $categoryRepository;
  21.     private CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory;
  22.     private CategoryDaoInterface $categoryDao;
  23.     private OfferPositionPaidDaoInterface $offerPositionPaidDao;
  24.     private OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory;
  25.     public function __construct(
  26.         MainMenuRepositoryInterface $mainMenuRepository,
  27.         CategoryRepositoryInterface $categoryRepository,
  28.         CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory,
  29.         CategoryDaoInterface $categoryDao,
  30.         OfferPositionPaidDaoInterface $offerPositionPaidDao,
  31.         OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory
  32.     ) {
  33.         $this->mainMenuRepository $mainMenuRepository;
  34.         $this->categoryRepository $categoryRepository;
  35.         $this->categoryWithGeoLocationsDtoFactory $categoryWithGeoLocationsDtoFactory;
  36.         $this->categoryDao $categoryDao;
  37.         $this->offerPositionPaidDao $offerPositionPaidDao;
  38.         $this->offerFeaturedPaidFactory $offerFeaturedPaidFactory;
  39.     }
  40.     public function __invoke(GetCategoriesWithGeoLocationsQuery $query): GetCategoriesWithGeoLocationsResponse
  41.     {
  42.         $cityId $query->getCityId();
  43.         $activeMeOnMapCategories $this->categoryRepository->findActiveMeOnMapCategoriesByCityId($cityId);
  44.         if (=== count($activeMeOnMapCategories)) {
  45.             $activeMeOnMapCategories array_map(
  46.                 fn (MainMenu $mainMenuItem): Category =>
  47.                 $this->categoryRepository->findCategoryByMainMenu($mainMenuItem),
  48.                 $this->mainMenuRepository->findWithActiveOfferCategoriesByCityId($cityId),
  49.             );
  50.         }
  51.         $activeMeOnMapFeaturedCategories $this->categoryRepository->findActiveMeOnMapFeaturedCategoriesByCityId($cityId);
  52.         if (=== count($activeMeOnMapFeaturedCategories)) {
  53.             $featuredOnMapFilterCategoriesId $this->categoryDao->getFeaturedOnMapFilterCategoriesId($query->getCityId());
  54.             $activeMeOnMapFeaturedCategories $this->categoryRepository->findByIds($featuredOnMapFilterCategoriesId);
  55.         } else {
  56.             $featuredOnMapFilterCategoriesId array_map(
  57.                 static fn(Category $category): int => $category->getID(),
  58.                 $activeMeOnMapFeaturedCategories,
  59.             );
  60.         }
  61.         return new GetCategoriesWithGeoLocationsResponse(
  62.             array_map([$this->categoryWithGeoLocationsDtoFactory'create'], $activeMeOnMapCategories),
  63.             array_map([$this->categoryWithGeoLocationsDtoFactory'create'], $activeMeOnMapFeaturedCategories),
  64.             $featuredOnMapFilterCategoriesId,
  65.             array_map([$this->offerFeaturedPaidFactory'create'], $this->offerPositionPaidDao->findAllByCityId($query->getCityId())),
  66.         );
  67.     }
  68. }