src/EventSubscriber/ESProfilePostTransformSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
  4. use App\Entity\Location\Station;
  5. use App\Entity\Profile\Profile;
  6. use App\Entity\Profile\ProfileService;
  7. use Doctrine\ORM\EntityNotFoundException;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use FOS\ElasticaBundle\Event\TransformEvent;
  11. class ESProfilePostTransformSubscriber implements EventSubscriberInterface
  12. {
  13.     const INDEX_RECOMMENDATIONS = 'recommendations';
  14.     const INDEX_DESCRIPTION = 'description';
  15.     const INDEX_KEY_SEARCH = 'key_search';
  16.     private $siteLanguages = ['en', 'ru'];
  17.     private array $rublevskieStations = [];
  18.     public function __construct(ParameterBagInterface $parameterBag)
  19.     {
  20.         $this->siteLanguages = $parameterBag->get('app.allowed_locales');
  21.         $this->rublevskieStations = require __DIR__ . '/../../config/rublevskie_stations.php';
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return array(
  26.             TransformEvent::POST_TRANSFORM => 'postTransformEntity',
  27.         );
  28.     }
  29.     public function postTransformEntity(TransformEvent $event): void
  30.     {
  31.         $document = $event->getDocument();
  32.         if ($event->getObject() instanceof Profile) {
  33.             /** @var Profile $profile */
  34.             $profile = $event->getObject();
  35.             switch ($document->getIndex()) {
  36.                 case self::INDEX_RECOMMENDATIONS:
  37.                     $document->set('selfie', $profile->hasSelfie());
  38.                     $document->set('video', $profile->hasVideo());
  39.                     $document->set('commented', $profile->isCommented());
  40.                     $document->set('withoutPrepayment', $profile->isWithoutPrepayment());
  41.                     $document->set('whatsapp', (bool)($profile->getMessengers()?->hasWhatsApp()));
  42.                     $document->set('telegram', (bool)($profile->getMessengers()?->hasTelegram()));
  43.                     $document->set('max', (bool)($profile->getMessengers()?->hasMax()));
  44.                     $document->set('rublevskie', $this->isRublevskie($profile));
  45.                     $document->set('primaryStationId', $profile->getPrimaryStation()?->getId());
  46.                     $stations = [];
  47.                     foreach ($profile->getStations()->toArray() as $k => $v) {
  48.                         if ($k == 0)
  49.                             continue;
  50.                         $stations[] = $profile->getStations()->get($k)->getId();
  51.                     }
  52.                     $document->set('secondaryStationsIds', $stations);
  53.                     $document->set('description', $this->translatableValueToAllLanguages($profile->getDescription()));
  54.                     break;
  55.                 case self::INDEX_DESCRIPTION:
  56.                     $text = $profile->getDescription()->getTranslation('ru');
  57.                     preg_match_all('/\w+/u', $text, $matches);
  58.                     $words3charsMin = array_filter($matches[0], function ($match): bool {
  59.                         return mb_strlen($match) >= 3;
  60.                     });
  61.                     $document->set('descRu', $text);
  62.                     $document->set('descRu3ChrWords', count($words3charsMin));
  63.                     $document->set('descRuWords', count($matches[0]));
  64.                     break;
  65.                 case self::INDEX_KEY_SEARCH:
  66.                     try {
  67.                         $cityName = $this->translatableValueToAllLanguages($profile->getCity()->getName());
  68.                     } catch (EntityNotFoundException) {
  69.                         $cityName = '';
  70.                     }
  71.                     $searchKeysData = ''
  72.                         . $this->getParamString($profile->getPhoneNumber())
  73.                         . $this->getParamString($this->translatableValueToAllLanguages($profile->getName()))
  74.                         . $this->getParamString($profile->getPersonParameters()->getAge())
  75.                         . $this->getParamString($cityName)
  76.                         . $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
  77.                             $district = $station->getDistrict();
  78.                             return $district ? $this->translatableValueToAllLanguages($district->getName()) : '';
  79.                         })->toArray()))
  80.                         . $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
  81.                             $county = $station->getCounty();
  82.                             return $county ? $this->translatableValueToAllLanguages($county->getName()) : '';
  83.                         })->toArray()))
  84.                         . $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
  85.                             return $this->translatableValueToAllLanguages($station->getName());
  86.                         })->toArray()))
  87.                         . $this->getParamString(implode(' ', $profile->getProvidedServices()->map(function (ProfileService $profileService): string {
  88.                             return $this->translatableValueToAllLanguages($profileService->getService()->getName());
  89.                         })->toArray()))
  90.                         . $this->getParamString($this->translatableValueToAllLanguages($profile->getDescription()));
  91.                     $searchKeysData = ltrim($searchKeysData, ' ; ');
  92.                     $document->set('searchKeysData', $searchKeysData);
  93.                     break;
  94.             }
  95.         }
  96.     }
  97.     private function isRublevskie(Profile $profile): bool
  98.     {
  99.         foreach ($profile->getStations() as $station) {
  100.             if (in_array($station->getUriIdentity(), $this->rublevskieStations, true)) {
  101.                 return true;
  102.             }
  103.         }
  104.         return false;
  105.     }
  106.     protected function translatableValueToAllLanguages(TranslatableValue $translatableValue): string
  107.     {
  108.         $str = '';
  109.         array_walk($this->siteLanguages, function ($language) use (&$str, $translatableValue): void {
  110.             $str .= ' ' . $translatableValue->getTranslation($language);
  111.         });
  112.         return $str;
  113.     }
  114.     protected function getParamString(string $param): string
  115.     {
  116.         return ' ; ' . $param;
  117.     }
  118. }