src/Controller/ProfileWidgetController.php line 22

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\Service\ProfileList;
  6. use App\Service\ProfileListSpecificationService;
  7. use App\Specification\Profile\ProfileIdNotIn;
  8. use Happyr\DoctrineSpecification\Logic\AndX;
  9. use Psr\Cache\CacheItemPoolInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Cache\ItemInterface;
  14. class ProfileWidgetController extends AbstractController
  15. {
  16.     const CACHE_ITEM_KEY_LIST_BY_CATEGORIES 'list_by_categories';
  17.     #[Route(path'/by-categories/'name'profile_widget.by_categories')]
  18.     public function listByCategories(
  19.         City $cityProfileListSpecificationService $profileListSpecificationServiceProfileList $profileListCacheItemPoolInterface $profileWidgetCache
  20.     ): Response
  21.     {
  22.         $profilesByCategories $profileWidgetCache->get(
  23.             self::CACHE_ITEM_KEY_LIST_BY_CATEGORIES,
  24.             function (ItemInterface $item) use ($city$profileListSpecificationService$profileList) {
  25.                 $categories = ['elite''approved''new'];
  26.                 $profilesByCategories = [];
  27.                 $profilesIds = [];
  28.                 $limit 6;
  29.                 foreach ($categories as $category) {
  30.                     $spec = match ($category) {
  31.                         'elite' => $profileListSpecificationService->listForEliteGirls($city),
  32.                         'approved' => $profileListSpecificationService->listApproved(),
  33.                         'new' => $profileListSpecificationService->listNew(),
  34.                         'default' => null,
  35.                     };
  36.                     if(null === $spec) {
  37.                         continue;
  38.                     }
  39.                     $spec $spec->spec();
  40.                     $specWithExclusion count($profilesIds) ? new AndX($spec, new ProfileIdNotIn($profilesIds)) : $spec;
  41.                     $profiles $profileList->listRandom($citynull$specWithExclusionnulltruenull, [Genders::FEMALE], false$limit);
  42.                     if(count($profiles) < $limit && $specWithExclusion !== $spec) {
  43.                         $profiles $profileList->listRandom($citynull$specnulltruenull, [Genders::FEMALE], false$limit);
  44.                     }
  45.                     $profilesByCategories[$category] = $profiles;
  46.                     foreach ($profiles as $profile) {
  47.                         $profilesIds[] = $profile->id;
  48.                     }
  49.                 }
  50.                 return $profilesByCategories;
  51.             }
  52.         );
  53.         return $this->render('ProfileWidget/list_by_categories/widget.html.twig', [
  54.             'profiles_by_categories' => $profilesByCategories,
  55.         ]);
  56.     }
  57. }