src/Service/ProfileList.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Bridge\Porpaginas\Doctrine\ORM\FakeORMQueryPage;
  4. use App\Entity\Location\City;
  5. use App\Entity\Profile\Genders;
  6. use App\Event\PaginatorPageTakenEvent;
  7. use App\Repository\ProfileRepository;
  8. use App\Repository\ReadModel\ProfileListingReadModel;
  9. use App\Specification\Profile\ICountIdSpec;
  10. use App\Specification\Profile\ISelectIdListSpec;
  11. use App\Specification\Profile\ProfileHasOneOfGenders;
  12. use App\Specification\Profile\ProfileIdINOrderedByINValues;
  13. use App\Specification\Profile\ProfileIdNotIn;
  14. use App\Specification\Profile\ProfileIsActive;
  15. use App\Specification\Profile\ProfileIsArchived;
  16. use App\Specification\Profile\ProfileIsLocatedInCountry;
  17. use App\Specification\Profile\ProfileIsLocated;
  18. use App\Specification\Profile\ProfileIsMasseur;
  19. use App\Specification\Profile\ProfileIsModerationPassed;
  20. use App\Specification\Profile\ProfileIsNotMasseur;
  21. use App\Specification\Profile\ProfileIsNotRejected;
  22. use App\Specification\QueryModifier\FreeProfilesFeatureArchivedProfileOrder;
  23. use App\Specification\QueryModifier\FreeProfilesFeatureProfileOrder;
  24. use App\Specification\Profile\ProfileIsHidden;
  25. use App\Specification\Profile\ProfileIsNotHidden;
  26. use App\Specification\QueryModifier\LimitResult;
  27. use App\Specification\QueryModifier\ProfileOrderedByCreated;
  28. use App\Specification\QueryModifier\ProfileOrderedByInactivated;
  29. use App\Specification\QueryModifier\ProfileOrderedByRandom;
  30. use App\Specification\QueryModifier\ProfileOrderedByUpdated;
  31. use App\Specification\QueryModifier\ProfileOrderedByStatus;
  32. use App\Specification\QueryModifier\ProfilePlacementHiding;
  33. use Doctrine\ORM\EntityManagerInterface;
  34. use Happyr\DoctrineSpecification\Filter\Filter;
  35. use Happyr\DoctrineSpecification\Logic\AndX;
  36. use Happyr\DoctrineSpecification\Spec;
  37. use Happyr\DoctrineSpecification\Specification\Specification;
  38. use Porpaginas\Page;
  39. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  40. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  41. use Symfony\Component\HttpFoundation\Request;
  42. use Symfony\Component\HttpFoundation\RequestStack;
  43. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  44. class ProfileList
  45. {
  46.     public const ORDER_BY_STATUS 'status';
  47.     public const ORDER_BY_UPDATED 'updated';
  48.     public const ORDER_NONE 'none';
  49.     private ?Request $request;
  50.     private int $perPageDefault;
  51.     private int $perPage;
  52.     private array $executedCountQueryData;
  53.     public function __construct(
  54.         private ProfileRepository $profileRepository,
  55.         RequestStack $requestStack,
  56.         private Features $features,
  57.         ParameterBagInterface $parameterBag,
  58.         private EventDispatcherInterface $eventDispatcher,
  59.         private EntityManagerInterface $entityManager,
  60.         private ProfileTopBoard $profileTopBoard
  61.     )
  62.     {
  63.         $this->request $requestStack->getCurrentRequest();
  64.         $this->perPageDefault intval($parameterBag->get('profile_list.per_page'));
  65.         $this->perPage $this->perPageDefault;
  66.     }
  67.     public function listActiveWithinCityOrderedByStatusWithSpecAvoidingTopPlacement(
  68.         City $city, ?Filter $spec, array $additionalSpecs null, array $genders = [Genders::FEMALE]
  69.     ): array|Page
  70.     {
  71.         return $this->listActiveWithinCityOrderedByStatusWithSpec($city$spec$additionalSpecs$genderstrue);
  72.     }
  73.     public function listActiveWithinCityOrderedByStatusWithSpec(
  74.         City $city, ?Filter $spec, array $additionalSpecs null, array $genders = [Genders::FEMALE], bool $avoidTopPlacement false
  75.     ): array|Page
  76.     {
  77.         return $this->list($citynull$spec$additionalSpecstruenullself::ORDER_BY_STATUS,
  78.             nulltruenull$genders$avoidTopPlacement);
  79.     }
  80.     public function listActiveWithinCityOrderedByStatusWithSpecLimited(
  81.         City $city, ?Filter $spec, array $additionalSpecs null, array $genders = [Genders::FEMALE], bool $avoidTopPlacement falseint $limit 0,
  82.     ): array|Page
  83.     {
  84.         return $this->list($citynull$spec$additionalSpecstruenullself::ORDER_BY_STATUS,
  85.             $limitfalsenull$genders$avoidTopPlacement);
  86.     }
  87.     public function listActiveNotMasseurWithinCityOrderedByStatusWithSpec(
  88.         City $city, ?Filter $spec, array $additionalSpecs null, array $genders = [Genders::FEMALE]
  89.     ): array|Page
  90.     {
  91.         return $this->list($citynull$spec$additionalSpecstruefalseself::ORDER_BY_STATUS,
  92.             nulltruenull$genders);
  93.     }
  94.     public function list(
  95.         City $city, ?string $country, ?Filter $spec, ?array $additionalSpecsbool $active, ?bool $masseur false,
  96.         ?string $order self::ORDER_BY_STATUS, ?int $limit nullbool $paged true, ?callable $fetchByIdMethod null,
  97.         array $genders = [Genders::FEMALE], bool $avoidTopPlacement false
  98.     ): array|Page
  99.     {
  100. //        $this->perPage = $limit ?? $this->perPageDefault;
  101.         $this->perPage $this->perPageDefault;
  102.         $topPlacementToAvoidId null;
  103.         if(/*null === $limit && */true === $avoidTopPlacement) {
  104.             $profileTopPlacement $this->profileTopBoard->currentTopPlacement(false);
  105.             if(null !== $profileTopPlacement) {
  106.                 $topPlacementToAvoidId $profileTopPlacement->getId();
  107.                 //на тесте часто ставят 1 на страницу, что ломает логику, т.к. после декремента становится 0 на страницу,
  108.                 //за тем и условие
  109.                 if($this->perPage 1) {
  110.                     $this->perPage--;
  111.                 }
  112.             }
  113.         }
  114.         $order $this->getOrderSpecByFlags($order$active);
  115.         $masseurSpec $this->getMasseurSpecByFlag($masseur);
  116.         $activeSpec $this->getActiveSpecByFlag($active);
  117.         $excludeTopProfileSpec null !== $topPlacementToAvoidId ? new ProfileIdNotIn([$topPlacementToAvoidId]) : null;
  118.         $criteria Spec::andX(
  119.             $country ProfileIsLocatedInCountry::withCountryCode($city->getCountryCode()) : ProfileIsLocated::withinCity($city),
  120.             $activeSpec
  121.         );
  122.         if($masseurSpec)
  123.             $criteria->andX($masseurSpec);
  124.         if(null !== $excludeTopProfileSpec)
  125.             $criteria->andX($excludeTopProfileSpec);
  126.         $criteria->andX($this->getModerationSpecByFlag());
  127.         $criteria->andX(new ProfileHasOneOfGenders($genders));
  128.         $criteriaForIdList = clone $criteria;
  129.         if($paged) {
  130.             $count $this->countOfCriteriaWithCustomSpec($criteria$spec$additionalSpecs);
  131.             if(== $count)
  132.                 return $this->takeFakePage($count, []);
  133.         }
  134.         if($order)
  135.             $criteriaForIdList->andX($order);
  136.         $idList $this->listIdOfCriteriaWithCustomSpec($criteriaForIdList$spec$additionalSpecs$paged$limit);
  137.         $profiles = [];
  138.         if(!empty($idList)) {
  139.             // $profiles = $this->profileRepository->matchingSpecRaw(new ProfileIdINOrderedByINValues($idList), null, true, array($this->profileRepository, 'hydrateProfileRow'));
  140.             $profiles null == $fetchByIdMethod
  141.                 $this->profileRepository->fetchListingByIds(new ProfileIdINOrderedByINValues($idList))
  142.                 : $fetchByIdMethod(new ProfileIdINOrderedByINValues($idList));
  143.         }
  144.         if($paged) {
  145.             $profiles $this->takeFakePage($count$profiles);
  146.         }
  147.         $this->restorePerPageToDefault();
  148.         return $profiles;
  149.     }
  150.     protected function countOfCriteriaWithCustomSpec(AndX $criteria, ?Filter $spec, array $additionalSpecs null): int
  151.     {
  152.         if(null == $additionalSpecs)
  153.             $additionalSpecs = [];
  154.         array_unshift($additionalSpecs$spec);
  155.         array_walk($additionalSpecs, function($spec) use ($criteria): void {
  156.             $criteria->andX($spec);
  157.             if($spec instanceof ICountIdSpec)
  158.                 $criteria->andX($spec->getCountIdSpec());
  159.         });
  160.         $defaultStack $this->entityManager->getConnection()->getConfiguration()->getSQLLogger();
  161.         $stack = new \Doctrine\DBAL\Logging\DebugStack();
  162.         $this->entityManager->getConnection()->getConfiguration()->setSQLLogger($stack);
  163.         $result $this->profileRepository->countMatchingSpec($criteria);
  164.         //$this->executedCountQueryData = ['sql' => 'SELECT count( p0_.id ) AS sclr_0 FROM `profiles` p0_ INNER JOIN profile_adboard_placements p1_ ON p0_.id = p1_.profile_id  WHERE ( p0_.deleted_at IS NULL )', 'params' => [], 'types' => []];//
  165.         $this->executedCountQueryData $stack->queries[count($stack->queries)];
  166.         $this->entityManager->getConnection()->getConfiguration()->setSQLLogger($defaultStack);
  167.         return $result;
  168.     }
  169.     protected function listIdOfCriteriaWithCustomSpec(AndX $criteria, ?Filter $spec, array $additionalSpecs nullbool $paged true, ?int $limit null): array
  170.     {
  171.         if(null == $additionalSpecs)
  172.             $additionalSpecs = [];
  173.         array_unshift($additionalSpecs$spec);
  174.         array_walk($additionalSpecs, function($spec) use ($criteria): void {
  175.             $criteria->andX($spec);
  176.             if($spec instanceof ISelectIdListSpec)
  177.                 $criteria->andX($spec->getIdListSpec());
  178.         });
  179.         return $this->profileRepository->listIdMatchingSpec(
  180.             $criteria,
  181.             $paged $this->getOffset() : 0,
  182.             $paged $this->perPage $limit
  183. //            $limit ?: $this->perPage
  184.         );
  185.     }
  186.     protected function getPage(): int
  187.     {
  188.         $page = (int)$this->request->get('page');
  189.         if ($page 1)
  190.             $page 1;
  191.         return $page;
  192.     }
  193.     protected function getOffset(): float|int
  194.     {
  195.         return ($this->getPage() - 1) * $this->perPage;
  196.     }
  197.     protected function takeFakePage(int $totalResults, array $profiles): Page
  198.     {
  199.         //если передана страница, которой нет в основной выборке
  200.         //if($totalResults != 0 && $this->getPage() > ceil($totalResults / $this->perPage))
  201.         if($totalResults && $this->getPage() != && $this->getOffset() > $totalResults 1)
  202.             throw new NotFoundHttpException('Page number doesn\'t exist');
  203.         $profileIds array_map(function(ProfileListingReadModel $profileListingReadModel): int {
  204.             return $profileListingReadModel->id;
  205.         }, $profiles);
  206.         $this->eventDispatcher->dispatch(new PaginatorPageTakenEvent($profiles), PaginatorPageTakenEvent::NAME);
  207.         return new FakeORMQueryPage($this->getOffset(), $this->getPage(), $this->perPage$totalResults$profiles);
  208.     }
  209.     public function listRandom(
  210.         City $city, ?string $country, ?Filter $spec, ?array $additionalSpecsbool $active, ?bool $masseur false,
  211.         array $genders = [Genders::FEMALE], bool $paged true, ?int $limit null
  212.     ): array|Page
  213.     {
  214.         $masseurSpec $this->getMasseurSpecByFlag($masseur);
  215.         $activeSpec $this->getActiveSpecByFlag($active);
  216.         $criteria Spec::andX(
  217.             $country ProfileIsLocatedInCountry::withCountryCode($city->getCountryCode()) : ProfileIsLocated::withinCity($city),
  218.             $activeSpec
  219.         );
  220.         if($masseurSpec)
  221.             $criteria->andX($masseurSpec);
  222.         $criteria->andX($this->getModerationSpecByFlag());
  223.         $criteria->andX(new ProfileHasOneOfGenders($genders));
  224.         $criteria->andX(new ProfileOrderedByRandom());
  225.         $idList $this->listIdOfCriteriaWithCustomSpec($criteria$spec$additionalSpecs$paged$limit);
  226.         $profiles = [];
  227.         if(!empty($idList)) {
  228.             $profiles $this->profileRepository->fetchListingByIds(new ProfileIdINOrderedByINValues($idList));
  229.         }
  230.         if($paged) {
  231.             $profiles $this->takeFakePage(count($profiles)/*$this->perPage*/$profiles);
  232.         }
  233.         return $profiles;
  234.     }
  235.     public function listRecent(City $cityint $count, array $genders = [Genders::FEMALE]): array
  236.     {
  237.         $criteria Spec::andX(
  238.             //new ProfileAdBoardPlacement(),
  239.             new ProfileIsActive(),
  240.             new ProfilePlacementHiding(),
  241.             new \App\Specification\QueryModifier\ProfileAvatar(),
  242.             // убрано чтобы не было мало записей на маленьких городах
  243.             // new ProfileIsNew(),
  244.             ProfileIsLocated::withinCity($city),
  245.             new ProfileOrderedByCreated(),
  246.             new LimitResult($count)
  247.         );
  248.         $criteria->andX($this->getActiveSpecByFlag(true));
  249.         $criteria->andX($this->getModerationSpecByFlag());
  250.         $criteria->andX(new ProfileHasOneOfGenders($genders));
  251.         $result $this->profileRepository->matchingSpecRaw($criterianullfalse);
  252.         return $result;
  253.     }
  254.     public function listBySpec(?Filter $spec, ?array $additionalSpecs null, ?int $limit nullbool $paged true, ?callable $fetchByIdMethod null): array|Page
  255.     {
  256.         $this->perPage $limit ?? $this->perPageDefault;
  257.         $criteria Spec::andX(
  258.             $spec
  259.         );
  260.         $criteriaForIdList = clone $criteria;
  261.         if($paged) {
  262.             $count $this->countOfCriteriaWithCustomSpec($criteria$spec$additionalSpecs);
  263.             if(== $count)
  264.                 return $this->takeFakePage($count, []);
  265.         }
  266.         $idList $this->listIdOfCriteriaWithCustomSpec($criteriaForIdList$spec$additionalSpecs$paged);
  267.         $profiles = [];
  268.         if(!empty($idList)) {
  269.             # $fetchByIdMethod передается если нужно, чтобы результат был не в виде ProfileListingReadModel, например.
  270.             # Как вариант - [$profileRepository, 'findByIds']
  271.             $profiles null == $fetchByIdMethod
  272.                 $this->profileRepository->fetchListingByIds(new ProfileIdINOrderedByINValues($idList))
  273.                 : $fetchByIdMethod($idList);
  274.         }
  275.         if($paged) {
  276.             $profiles $this->takeFakePage($count$profiles);
  277.         }
  278.         $this->restorePerPageToDefault();
  279.         return $profiles;
  280.     }
  281.     protected function getMasseurSpecByFlag(?bool $masseur): ?Specification
  282.     {
  283.         if(true === $masseur) {
  284.             $masseurSpec = new ProfileIsMasseur();
  285.         } else if(false === $masseur) {
  286.             $masseurSpec = new ProfileIsNotMasseur();
  287.         } else {
  288.             $masseurSpec null;
  289.         }
  290.         return $masseurSpec;
  291.     }
  292.     public function getActiveSpecByFlag(bool $active): ProfileIsHidden|ProfileIsArchived|ProfileIsNotHidden|ProfileIsActive
  293.     {
  294.         if($active) {
  295.             $activeSpec $this->features->free_profiles() ? new ProfileIsNotHidden() : new ProfileIsActive();
  296.         } else {
  297.             $activeSpec $this->features->free_profiles() ? new ProfileIsHidden() : new ProfileIsArchived();
  298.         }
  299.         return $activeSpec;
  300.     }
  301.     public function getModerationSpecByFlag(): ProfileIsModerationPassed|ProfileIsNotRejected
  302.     {
  303.         return $this->features->hard_moderation() ? new ProfileIsModerationPassed() : new ProfileIsNotRejected();
  304.     }
  305.     protected function getOrderSpecByFlags(string $orderbool $active): string|ProfileOrderedByStatus|ProfileOrderedByUpdated|ProfileOrderedByInactivated|FreeProfilesFeatureProfileOrder|FreeProfilesFeatureArchivedProfileOrder
  306.     {
  307.         switch($order) {
  308.             case self::ORDER_BY_UPDATED:
  309.                 $defaultOrder = new ProfileOrderedByUpdated();
  310.                 break;
  311.             case self::ORDER_NONE:
  312.                 $defaultOrder null;
  313.                 break;
  314.             case self::ORDER_BY_STATUS:
  315.             default:
  316.                 $defaultOrder = new ProfileOrderedByStatus();
  317.                 break;
  318.         }
  319.         if(null != $defaultOrder) {
  320.             if ($this->features->free_profiles()) {
  321.                 $order $active ? new FreeProfilesFeatureProfileOrder($this->features->consider_approved_priority()) : new FreeProfilesFeatureArchivedProfileOrder();
  322.             } else {
  323.                 $order $active $defaultOrder : new ProfileOrderedByInactivated();
  324.             }
  325.         }
  326.         return $order;
  327.     }
  328.     public function executedCountQueryData(): array
  329.     {
  330.         return $this->executedCountQueryData;
  331.     }
  332.     protected function restorePerPageToDefault(): void
  333.     {
  334.         $this->perPage $this->perPageDefault;
  335.     }
  336. }