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('primaryStationId'$profile->getPrimaryStation()?->getId());
  38.                     $stations = [];
  39.                     foreach ($profile->getStations()->toArray() as $k => $v) {
  40.                         if ($k == 0)
  41.                             continue;
  42.                         $stations[] = $profile->getStations()->get($k)->getId();
  43.                     }
  44.                     $document->set('secondaryStationsIds'$stations);
  45.                     $document->set('description'$this->translatableValueToAllLanguages($profile->getDescription()));
  46.                     break;
  47.                 case self::INDEX_DESCRIPTION:
  48.                     $text $profile->getDescription()->getTranslation('ru');
  49.                     preg_match_all('/\w+/u'$text$matches);
  50.                     $words3charsMin array_filter($matches[0], function ($match): bool {
  51.                         return mb_strlen($match) >= 3;
  52.                     });
  53.                     $document->set('descRu'$text);
  54.                     $document->set('descRu3ChrWords'count($words3charsMin));
  55.                     $document->set('descRuWords'count($matches[0]));
  56.                     break;
  57.                 case self::INDEX_KEY_SEARCH:
  58.                     $searchKeysData ''
  59.                         $this->getParamString($profile->getPhoneNumber())
  60.                         . $this->getParamString($this->translatableValueToAllLanguages($profile->getName()))
  61.                         . $this->getParamString($profile->getPersonParameters()->getAge())
  62.                         . $this->getParamString($this->translatableValueToAllLanguages($profile->getCity()->getName()))
  63.                         . $this->getParamString(implode(' '$profile->getStations()->map(function (Station $station): string {
  64.                             $district $station->getDistrict();
  65.                             return $district $this->translatableValueToAllLanguages($district->getName()) : '';
  66.                         })->toArray()))
  67.                         . $this->getParamString(implode(' '$profile->getStations()->map(function (Station $station): string {
  68.                             $county $station->getCounty();
  69.                             return $county $this->translatableValueToAllLanguages($county->getName()) : '';
  70.                         })->toArray()))
  71.                         . $this->getParamString(implode(' '$profile->getStations()->map(function (Station $station): string {
  72.                             return $this->translatableValueToAllLanguages($station->getName());
  73.                         })->toArray()))
  74.                         . $this->getParamString(implode(' '$profile->getProvidedServices()->map(function (ProfileService $profileService): string {
  75.                             return $this->translatableValueToAllLanguages($profileService->getService()->getName());
  76.                         })->toArray()))
  77.                         . $this->getParamString($this->translatableValueToAllLanguages($profile->getDescription()));
  78.                     $searchKeysData ltrim($searchKeysData' ; ');
  79.                     $document->set('searchKeysData'$searchKeysData);
  80.                     break;
  81.             }
  82.         }
  83.     }
  84.     protected function translatableValueToAllLanguages(TranslatableValue $translatableValue): string
  85.     {
  86.         $str '';
  87.         array_walk($this->siteLanguages, function ($language) use (&$str$translatableValue): void {
  88.             $str .= ' ' $translatableValue->getTranslation($language);
  89.         });
  90.         return $str;
  91.     }
  92.     protected function getParamString(string $param): string
  93.     {
  94.         return ' ; ' $param;
  95.     }
  96. }