src/Controller/CityListController.php line 59

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-15
  5.  * Time: 12:36
  6.  */
  7. namespace App\Controller;
  8. use App\Entity\Location\City;
  9. use App\Repository\CityRepository;
  10. use App\Repository\ProfileRepository;
  11. use App\Service\DefaultCityProvider;
  12. use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Intl\Countries;
  17. use Symfony\Component\Intl\Intl;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class CityListController extends AbstractController
  20. {
  21.     public function __construct(
  22.         private CityRepository $cityRepository,
  23.         private ProfileRepository $profileRepository,
  24.         private TranslatorInterface $translator
  25.     ) {}
  26.     public function page(): Response
  27.     {
  28.         $countByCity $this->profileRepository->countByCity();
  29.         $cities = [
  30.             'megalopolis' => [],
  31.             'moscow_region' => [],
  32.         ];
  33.         foreach ($this->cityRepository->findAll() as $city) {
  34.             /** @var City $city */
  35.             $label sprintf('%s (%d)'$this->translator->trans($city->getName()), $countByCity[$city->getId()] ?? 0);
  36.             if (City::GROUP_MEGALOPOLIS === $city->getCityGroup()) {
  37.                 $cities['megalopolis'][$label] = $city;
  38.             } elseif (City::GROUP_MOSCOW_REGION === $city->getCityGroup()) {
  39.                 $cities['moscow_region'][$label] = $city;
  40.             } else {
  41.                 $cities[$city->getCountryCode()][$label] = $city;
  42.             }
  43.         }
  44.         return $this->render('CityList/list.html.twig', [
  45.             'cities' => $cities,
  46.         ]);
  47.     }
  48.     /**
  49.      * @Feature("has_city_list_page")
  50.      */
  51.     public function listByCountry(Request $requestDefaultCityProvider $defaultCityProvider): Response
  52.     {
  53.         $locale $request->getLocale() ?? 'ru';
  54.         $countries Countries::getNames($locale);
  55.         $countByCity $this->profileRepository->countByCity();
  56.         $cities = [];
  57.         foreach ($this->cityRepository->findAll() as $city) {
  58.             /** @var City $city */
  59.             $countryCode $city->getCountryCode();
  60.             $country $countries[$countryCode];
  61.             if (!isset($cities[$country]))
  62.                 $cities[$country] = [];
  63.             $route $city->equals($defaultCityProvider->getDefaultCity()) ? 'homepage' 'profile_list.list_by_city';
  64.             $cities[$country][$city->getId()] = [
  65.                 'city' => $city,
  66.                 'count' => $countByCity[$city->getId()] ?? 0,
  67.                 'uri' => $this->generateUrl($route, ['city' => $city->getUriIdentity()]),
  68.             ];
  69.         }
  70.         return $this->render('CityList/list_by_country.html.twig', [
  71.             'cities' => $cities,
  72.         ]);
  73.     }
  74. }