src/EventSubscriber/ESProfilePostTransformSubscriber.php line 35

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