src/Service/ProfileList.php line 383

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