src/EventSubscriber/ESProfilePostTransformSubscriber.php line 33

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