src/Menu/CityStationsMenuBuilder.php line 34

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-21
  5.  * Time: 21:24
  6.  */
  7. namespace App\Menu;
  8. use App\Entity\Location\City;
  9. use App\Repository\ReadModel\StationLineReadModel;
  10. use App\Repository\StationRepository;
  11. use App\Service\DefaultCityProvider;
  12. use App\Service\EntitySortService;
  13. use App\Service\LocationsProfileCounters;
  14. use Knp\Menu\FactoryInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class CityStationsMenuBuilder
  18. {
  19.     const NAME 'city_stations';
  20.     public function __construct(
  21.         private FactoryInterface $factory,
  22.         private RequestStack $requestStack,
  23.         private DefaultCityProvider $defaultCityProvider,
  24.         private TranslatorInterface $translator,
  25.         private EntitySortService $sortService,
  26.         private StationRepository $stationRepository,
  27.         private LocationsProfileCounters $locationsProfileCounters,
  28.     ) {}
  29.     public function build(array $options)
  30.     {
  31.         $countByStations $this->locationsProfileCounters->getCountByStations();
  32.         $menu $this->factory->createItem(self::NAME);
  33.         $currentCity $this->getCurrentCity();
  34.         if ($currentCity->equals($this->defaultCityProvider->getDefaultCity())) {
  35.             $options = [
  36.                 'route' => 'homepage',
  37.             ];
  38.         } else {
  39.             $options = [
  40.                 'route'           => 'profile_list.list_by_city',
  41.                 'routeParameters' => ['city' => $currentCity->getUriIdentity()],
  42.             ];
  43.         }
  44.         $menu->addChild('all_stations'$options);
  45.         $stations $currentCity->getStations();
  46.         $stations $this->sortService->sortEntityArrayByTranslatableFieldName(iterator_to_array($stations->getIterator()), 'name');
  47.         foreach ($stations as $station) {
  48.             $label sprintf('%s (%d)'$this->translator->trans($station->getName()), $countByStations[$station->getId()] ?? 0);
  49.             $menu->addChild($label, [
  50.                 'route' => 'profile_list.list_by_station',
  51.                 'routeParameters' => [
  52.                     'city' => $currentCity->getUriIdentity(),
  53.                     'station' => $station->getUriIdentity(),
  54.                 ],
  55.                 'extras' => [
  56.                     'lines' => array_map(function(StationLineReadModel $line): array { return [
  57.                         'name' => $line->name,
  58.                         'color' => $line->color,
  59.                     ]; }, $this->stationRepository->stationLinesCached($station)),
  60.                 ],
  61.             ]);
  62.         }
  63.         return $menu;
  64.     }
  65.     private function getCurrentCity(): City
  66.     {
  67.         $request $this->requestStack->getCurrentRequest();
  68.         $city $request->attributes->get('city');
  69.         if (null === $city) {
  70.             $city $this->defaultCityProvider->getDefaultCity();
  71.         }
  72.         return $city;
  73.     }
  74. }