src/EventSubscriber/ESProfilePostTransformSubscriber.php line 25

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