src/Service/SearchByKeywordsService.php line 86

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\Form\SearchByKeywordsForm;
  6. use App\Repository\ProfileRepository;
  7. use App\Repository\SearchByKeywordsESRepository;
  8. use App\Specification\Profile\ProfileIdINOrderedByINValues;
  9. use Porpaginas\Page;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\Form\FormFactoryInterface;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  17. class SearchByKeywordsService
  18. {
  19.     private ?Request $request;
  20.     private int $perPage;
  21.     public function __construct(
  22.         RequestStack $requestStack,
  23.         ParameterBagInterface $parameterBag,
  24.         private SearchByKeywordsESRepository $searchByKeywordsESRepository,
  25.         private FormFactoryInterface $formFactory,
  26.         private UrlGeneratorInterface $urlGenerator,
  27.         private DefaultCityProvider $defaultCityProvider,
  28.         private PhoneNumberService $phoneNumberService,
  29.         private ProfileRepository $profileRepository
  30.     )
  31.     {
  32.         $this->request $requestStack->getCurrentRequest();
  33.         $this->perPage $parameterBag->get('search_profiles_by_keywords.per_page');
  34.     }
  35.     public function searchByKeywords(City $citystring $keywords): Page
  36.     {
  37.         $keywords preg_replace("/[^\w0-9 -\(\)\+]/u"""$keywords);
  38.         $keywords preg_replace_callback('/\+?[\d\s\)\(\-]{5,25}/', function($matches) use ($city): string {
  39. //            return ' '.str_replace(['-','+7'], ['', ''], $this->phoneNumberService->getInternationalNumber(trim($matches[0]), $city)).' ';
  40.             return ' '.$this->phoneNumberService->getInternationalNumber(trim($matches[0]), $city).' ';
  41.         }, $keywords);
  42.         $profileIds $this->searchByKeywordsESRepository->searchProfileIdsByKeywords($city$keywords$this->getOffset(), $this->perPage);
  43.         $profiles = !empty($profileIds) ? $this->profileRepository->fetchListingByIds(new ProfileIdINOrderedByINValues($profileIds)) : [];
  44.         return $this->takeFakePage($this->searchByKeywordsESRepository->getTotal(), $profiles);
  45.     }
  46.     protected function getPage(): int
  47.     {
  48.         $page = (int)$this->request->get('page');
  49.         if ($page 1)
  50.             $page 1;
  51.         return $page;
  52.     }
  53.     protected function getOffset(): int|float
  54.     {
  55.         return ($this->getPage() - 1) * $this->perPage;
  56.     }
  57.     protected function takeFakePage(int $totalResults, array $profiles): Page
  58.     {
  59.         $pageCount ceil($totalResults $this->perPage);
  60.         if($pageCount && $this->getPage() > $pageCount)
  61.             throw new NotFoundHttpException('Wrong page number');
  62.         return new FakeORMQueryPage($this->getOffset(), $this->getPage(), $this->perPage$totalResults$profiles);
  63.     }
  64.     public function getLastQuery(): ?array
  65.     {
  66.         return $this->searchByKeywordsESRepository->getLastQuery();
  67.     }
  68.     public function getScores(): array
  69.     {
  70.         return $this->searchByKeywordsESRepository->getScores();
  71.     }
  72.     public function getSearchByKeywordsForm(): FormInterface
  73.     {
  74.         $city $this->request->attributes->get('city');
  75.         if (null === $city) {
  76.             $city $this->defaultCityProvider->getDefaultCity();
  77.         }
  78.         $form $this->formFactory->create(SearchByKeywordsForm::class, null, [
  79.             'action'          => $this->urlGenerator->generate('search_by_keywords.page', ['city' => $city->getUriIdentity()]),
  80.             'csrf_protection' => false,
  81.         ]);
  82.         return $form;
  83.     }
  84. }