src/Controller/RecentProfilesController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Location\City;
  4. use App\Entity\Profile\Genders;
  5. use App\Entity\Profile\Profile;
  6. use App\Repository\ProfileRepository;
  7. use App\Service\DefaultCityProvider;
  8. use App\Service\ProfileList;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Routing\RouterInterface;
  18. class RecentProfilesController extends AbstractController
  19. {
  20.     const DEFAULT_MAX_ENTRIES = 3;
  21.     private Request $request;
  22.     public function __construct(
  23.         RequestStack $requestStack,
  24.         private DefaultCityProvider $defaultCityProvider,
  25.         private RouterInterface $router,
  26.         private ProfileList $profileList,
  27.         private ProfileRepository $profileRepository,
  28.     )
  29.     {
  30.         $this->request = $requestStack->getCurrentRequest();
  31.     }
  32.     #[Route("/recently-viewed", name: "recent_profiles.recently_viewed.list", methods: ["POST"])]
  33.     public function recentlyViewedList(): Response
  34.     {
  35.         if(false === $this->request->isXmlHttpRequest()) {
  36.             throw new BadRequestHttpException();
  37.         }
  38.         $data = $this->request->getContent();
  39.         $data = json_decode($data, true);
  40.         $profiles = $data['recently_viewed_profiles'] ?? [];
  41.         $profiles = $this->profileRepository->findByIds($profiles);
  42.         return $this->render('RecentProfiles/recently_viewed.list.html.twig', [
  43.             'profiles' => $profiles,
  44.         ]);
  45.     }
  46.     #[Route("/{city}/{count}/{gender}", name: "recent_profiles.list", methods: ["POST"], defaults: ["gender" => 1])]
  47.     #[ParamConverter("city", converter:"city_converter")]
  48.     public function list(City $city, int $count = self::DEFAULT_MAX_ENTRIES, int $gender = Genders::FEMALE): Response
  49.     {
  50.         $genders = ($gender == Genders::FEMALE) ? [Genders::FEMALE] : [/*Genders::FEMALE, */$gender];
  51.         $city = $this->request->get('city') ?? $this->defaultCityProvider->getDefaultCity();
  52.         $locale = $this->request->get('locale') ?? 'ru';
  53.         $result = $this->profileList->listRecent($city, $count, $genders);
  54.         $profiles = array_map(function(Profile $profile) use ($locale): array {
  55.             return [
  56.                 'name' => $profile->getName()->getTranslation($locale),
  57.                 'uri' => $this->router->generate('profile_preview.page', ['city' => $profile->getCity()->getUriIdentity(), 'profile' => $profile->getUriIdentity()]),
  58.             ];
  59.         }, $result);
  60.         return new JsonResponse($profiles);
  61.     }
  62. }