<?php
declare(strict_types=1);
namespace Slivki\MessageHandler\Query\MeOnMap;
use Slivki\Dao\Category\CategoryDaoInterface;
use Slivki\Dao\MeOnMap\OfferPositionPaidDaoInterface;
use Slivki\Entity\Category;
use Slivki\Entity\MainMenu;
use Slivki\Factory\Category\CategoryWithGeoLocationsDtoFactoryInterface;
use Slivki\Factory\MeOnMap\OfferFeaturedPaidFactoryInterface;
use Slivki\Message\Query\MeOnMap\GetCategoriesWithGeoLocationsQuery;
use Slivki\Repository\Category\CategoryRepositoryInterface;
use Slivki\Repository\MainMenu\MainMenuRepositoryInterface;
use Slivki\Response\MeOnMap\GetCategoriesWithGeoLocationsResponse;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use function array_map;
use function count;
final class GetCategoriesWithGeoLocationsHandler implements MessageHandlerInterface
{
private MainMenuRepositoryInterface $mainMenuRepository;
private CategoryRepositoryInterface $categoryRepository;
private CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory;
private CategoryDaoInterface $categoryDao;
private OfferPositionPaidDaoInterface $offerPositionPaidDao;
private OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory;
public function __construct(
MainMenuRepositoryInterface $mainMenuRepository,
CategoryRepositoryInterface $categoryRepository,
CategoryWithGeoLocationsDtoFactoryInterface $categoryWithGeoLocationsDtoFactory,
CategoryDaoInterface $categoryDao,
OfferPositionPaidDaoInterface $offerPositionPaidDao,
OfferFeaturedPaidFactoryInterface $offerFeaturedPaidFactory
) {
$this->mainMenuRepository = $mainMenuRepository;
$this->categoryRepository = $categoryRepository;
$this->categoryWithGeoLocationsDtoFactory = $categoryWithGeoLocationsDtoFactory;
$this->categoryDao = $categoryDao;
$this->offerPositionPaidDao = $offerPositionPaidDao;
$this->offerFeaturedPaidFactory = $offerFeaturedPaidFactory;
}
public function __invoke(GetCategoriesWithGeoLocationsQuery $query): GetCategoriesWithGeoLocationsResponse
{
$cityId = $query->getCityId();
$activeMeOnMapCategories = $this->categoryRepository->findActiveMeOnMapCategoriesByCityId($cityId);
if (0 === count($activeMeOnMapCategories)) {
$activeMeOnMapCategories = array_map(
fn (MainMenu $mainMenuItem): Category =>
$this->categoryRepository->findCategoryByMainMenu($mainMenuItem),
$this->mainMenuRepository->findWithActiveOfferCategoriesByCityId($cityId),
);
}
$activeMeOnMapFeaturedCategories = $this->categoryRepository->findActiveMeOnMapFeaturedCategoriesByCityId($cityId);
if (0 === count($activeMeOnMapFeaturedCategories)) {
$featuredOnMapFilterCategoriesId = $this->categoryDao->getFeaturedOnMapFilterCategoriesId($query->getCityId());
$activeMeOnMapFeaturedCategories = $this->categoryRepository->findByIds($featuredOnMapFilterCategoriesId);
} else {
$featuredOnMapFilterCategoriesId = array_map(
static fn(Category $category): int => $category->getID(),
$activeMeOnMapFeaturedCategories,
);
}
return new GetCategoriesWithGeoLocationsResponse(
array_map([$this->categoryWithGeoLocationsDtoFactory, 'create'], $activeMeOnMapCategories),
array_map([$this->categoryWithGeoLocationsDtoFactory, 'create'], $activeMeOnMapFeaturedCategories),
$featuredOnMapFilterCategoriesId,
array_map([$this->offerFeaturedPaidFactory, 'create'], $this->offerPositionPaidDao->findAllByCityId($query->getCityId())),
);
}
}