src/Controller/SaloonListController.php line 86

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-15
  5.  * Time: 19:51
  6.  */
  7. namespace App\Controller;
  8. use AngelGamez\PorpaginasBundle\Controller\PaginationTrait;
  9. use App\Entity\Location\City;
  10. use App\Repository\DistrictRepository;
  11. use App\Repository\SaloonRepository;
  12. use App\Service\CountryCurrencyResolver;
  13. use App\Service\Features;
  14. use App\Service\ProfileListingRecommendationsFiller;
  15. use App\Service\ResponsiveAssetsService;
  16. use App\Specification\QueryModifier\FreeProfilesFeatureSaloonOrder;
  17. use App\Specification\Saloon\SaloonIsNotHidden;
  18. use App\Specification\QueryModifier\SaloonOrderedByStatus;
  19. use App\Specification\Saloon\SaloonInMoskowDistricts;
  20. use App\Specification\Saloon\SaloonIsActive;
  21. use App\Specification\Saloon\SaloonIsLocated;
  22. use App\Specification\Saloon\SaloonIsVip;
  23. use Flagception\Bundle\FlagceptionBundle\Annotations\Feature;
  24. use Happyr\DoctrineSpecification\Spec;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  27. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\RequestStack;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use OpenApi\Attributes as OA;
  33. use Porpaginas\Page;
  34. class SaloonListController extends AbstractController
  35. {
  36.     use PaginationTrait;
  37.     use ResponseTrait;
  38.     const ENTRIES_ON_PAGE 36;
  39.     private mixed $perPage;
  40.     public function __construct(
  41.         private SaloonRepository $saloonRepository,
  42.         private DistrictRepository $districtRepository,
  43.         private Features $features,
  44.         private RequestStack $requestStack,
  45.         private ResponsiveAssetsService $responsiveAssetsService,
  46.         private ProfileListingRecommendationsFiller $profileListingRecommendationsFiller,
  47.         ParameterBagInterface $parameterBag
  48.     )
  49.     {
  50.         $this->perPage $parameterBag->get('saloon_list.per_page');
  51.     }
  52.     /**
  53.      * @Feature("has_saloons")
  54.      */
  55.     #[ParamConverter('city'converter'city_converter')]
  56.     #[Route("/api/saloons/listing/city/{city}"name"api.saloon_listing.by_city"methods"GET"format"json")]
  57.     #[OA\Parameter(name"page"in"query"schema: new OA\Schema(type"integer", default: 1minimum1))]
  58.     #[OA\Tag(name"SaloonListing")]
  59.     #[OA\Response(
  60.         response200,
  61.         description'',
  62.         content: new OA\JsonContent(ref"#/components/schemas/SaloonListingPage")
  63.     )]
  64.     public function listByCity(Request $requestCity $city): Response
  65.     {
  66.         $criteria Spec::andX(
  67.             $this->features->free_profiles() ? new SaloonIsNotHidden() : new SaloonIsActive(),
  68.             SaloonIsLocated::withinCity($city),
  69.             $this->features->free_profiles() ? new FreeProfilesFeatureSaloonOrder() : new SaloonOrderedByStatus()
  70.         );
  71.         $result $this->saloonRepository->matchingSpec($criteria);
  72.         $saloons $this->takePage($result$this->perPage);
  73.         $recommendationsFill $this->getRecommendationsFill($city$saloons);
  74.         if ($this->isApiRequest($request)) {
  75.             return $this->json($saloonscontext: ['groups' => ['listing']]);
  76.         }
  77.         return $this->render('SaloonList/list.html.twig', [
  78.             'saloons' => $saloons,
  79.             'recommended_profiles' => $recommendationsFill['profiles'],
  80.             'ssr_recommended_profiles' => $recommendationsFill['ssr_profiles'],
  81.             'recommendations_fill_applied' => $recommendationsFill['applied'],
  82.             'recommendations_fill_original_count' => $recommendationsFill['original_count'],
  83.         ]);
  84.     }
  85.     /**
  86.      * @Feature("has_saloons")
  87.      */
  88.     #[ParamConverter('city'converter'city_converter')]
  89.     public function listForVipRelax(CountryCurrencyResolver $countryCurrencyResolverRequest $requestCity $city): Response
  90.     {
  91.         $minPrice $countryCurrencyResolver->getValueByCountryCode($city->getCountryCode(), [
  92.             'RUB' => 4000,
  93.             'UAH' => 1200,
  94.             'USD' => 100,
  95.             'EUR' => 100,
  96.         ]);
  97.         $districts = [];
  98.         foreach (SaloonInMoskowDistricts::$districtsUri as $uri) {
  99.             if (null !== $district $this->districtRepository->ofUriIdentityWithinCity($uri$city)) {
  100.                 $districts[] = $district;
  101.             }
  102.         }
  103.         if (!empty($districts)) {
  104.             $criteria Spec::andX(
  105.                 $this->features->free_profiles() ? new SaloonIsNotHidden() : new SaloonIsActive(),
  106.                 SaloonIsLocated::withinCity($city),
  107.                 new SaloonIsVip($city$districts$minPrice),
  108.                 $this->features->free_profiles() ? new FreeProfilesFeatureSaloonOrder() : new SaloonOrderedByStatus()
  109.             );
  110.             $result $this->saloonRepository->matchingSpec($criteria);
  111.             $saloons $this->takePage($result$this->perPage);
  112.         } else {
  113.             $saloons $this->emptyPagination();
  114.         }
  115.         $recommendationsFill $this->getRecommendationsFill($city$saloons);
  116.         return $this->render('SaloonList/list.html.twig', [
  117.             'saloons' => $saloons,
  118.             'recommended_profiles' => $recommendationsFill['profiles'],
  119.             'ssr_recommended_profiles' => $recommendationsFill['ssr_profiles'],
  120.             'recommendations_fill_applied' => $recommendationsFill['applied'],
  121.             'recommendations_fill_original_count' => $recommendationsFill['original_count'],
  122.         ]);
  123.     }
  124.     private function getRecommendationsFill(City $cityPage $saloons): array
  125.     {
  126.         $fill $this->profileListingRecommendationsFiller->recommendedProfilesForPage($city$saloons);
  127.         $fill['ssr_profiles'] = !$fill['applied']
  128.             ? $this->profileListingRecommendationsFiller->popularProfilesForBlock($city$saloons)
  129.             : [];
  130.         return $fill;
  131.     }
  132.     protected function render(string $view, array $parameters = [], Response $response null): Response
  133.     {
  134.         if($this->requestStack->getCurrentRequest()->getMethod() == 'POST') {
  135.             $response parent::render('SaloonList/list.saloons.html.twig'$parameters$response);
  136.             $content preg_replace('/\s{2,}/'' '$response->getContent());
  137.             $response->setContent($content);
  138.             return $response;
  139.         } else {
  140.             return parent::render($view$parameters$response);
  141.         }
  142.     }
  143. }