src/Menu/LocationsCountersMenuBuilder.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Menu;
  3. use App\Entity\Location\City;
  4. use App\Service\DefaultCityProvider;
  5. use App\Service\EntitySortService;
  6. use App\Service\LocationsProfileCounters;
  7. use Knp\Menu\FactoryInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. class LocationsCountersMenuBuilder
  11. {
  12.     const NAME 'locations_counters';
  13.     public function __construct(
  14.         private FactoryInterface $factory,
  15.         private RequestStack $requestStack,
  16.         private DefaultCityProvider $defaultCityProvider,
  17.         private TranslatorInterface $translator,
  18.         private EntitySortService $sortService,
  19.         private LocationsProfileCounters $locationsProfileCounters,
  20.     ) {}
  21.     public function build(array $options)
  22.     {
  23.         $menu $this->factory->createItem(self::NAME);
  24.         $currentCity $this->getCurrentCity();
  25.         $stationsItem $menu->addChild('stations');
  26.         $countByStations $this->locationsProfileCounters->getCountByStations();
  27.         $stations $currentCity->getStations();
  28.         $stations $this->sortService->sortEntityArrayByTranslatableFieldName(iterator_to_array($stations->getIterator()), 'name');
  29.         foreach ($stations as $station) {
  30.             $label sprintf('%s (%d)'$this->translator->trans($station->getName()), $countByStations[$station->getId()] ?? 0);
  31.             $stationsItem->addChild($label, [
  32.                 'route' => 'profile_list.list_by_station',
  33.                 'routeParameters' => [
  34.                     'city' => $currentCity->getUriIdentity(),
  35.                     'station' => $station->getUriIdentity(),
  36.                 ],
  37.             ]);
  38.         }
  39.         $districtsItem $menu->addChild('districts');
  40.         $countByDistricts $this->locationsProfileCounters->getCountByDistricts();
  41.         $districts $currentCity->getDistricts();
  42.         $districts $this->sortService->sortEntityArrayByTranslatableFieldName(iterator_to_array($districts->getIterator()), 'name');
  43.         foreach ($districts as $district) {
  44.             $label sprintf('%s (%d)'$this->translator->trans($district->getName()), $countByDistricts[$district->getId()] ?? 0);
  45.             $districtsItem->addChild($label, [
  46.                 'route' => 'profile_list.list_by_district',
  47.                 'routeParameters' => [
  48.                     'city' => $currentCity->getUriIdentity(),
  49.                     'district' => $district->getUriIdentity(),
  50.                 ],
  51.             ]);
  52.         }
  53.         $countiesItem $menu->addChild('counties');
  54.         $countByCounties $this->locationsProfileCounters->getCountByCounties();
  55.         $counties $currentCity->getCounties();
  56.         $counties $this->sortService->sortEntityArrayByTranslatableFieldName(iterator_to_array($counties->getIterator()), 'name');
  57.         foreach ($counties as $county) {
  58.             $label sprintf('%s (%d)'$this->translator->trans($county->getName()), $countByCounties[$county->getId()] ?? 0);
  59.             $countiesItem->addChild($label, [
  60.                 'route' => 'profile_list.list_by_county',
  61.                 'routeParameters' => [
  62.                     'city' => $currentCity->getUriIdentity(),
  63.                     'county' => $county->getUriIdentity(),
  64.                 ],
  65.             ]);
  66.         }
  67.         return $menu;
  68.     }
  69.     private function getCurrentCity(): City
  70.     {
  71.         $request $this->requestStack->getCurrentRequest();
  72.         $city $request->attributes->get('city');
  73.         if (null === $city) {
  74.             $city $this->defaultCityProvider->getDefaultCity();
  75.         }
  76.         return $city;
  77.     }
  78. }