<?php
namespace App\EventSubscriber;
use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
use App\Entity\Location\Station;
use App\Entity\Profile\Profile;
use App\Entity\Profile\ProfileService;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\ElasticaBundle\Event\TransformEvent;
class ESProfilePostTransformSubscriber implements EventSubscriberInterface
{
const INDEX_RECOMMENDATIONS = 'recommendations';
const INDEX_DESCRIPTION = 'description';
const INDEX_KEY_SEARCH = 'key_search';
private $siteLanguages = ['en', 'ru'];
public function __construct(ParameterBagInterface $parameterBag)
{
$this->siteLanguages = $parameterBag->get('app.allowed_locales');
}
public function postTransformEntity(TransformEvent $event): void
{
$document = $event->getDocument();
if ($event->getObject() instanceof Profile) {
/** @var Profile $profile */
$profile = $event->getObject();
switch($document->getIndex()) {
case self::INDEX_RECOMMENDATIONS:
$document->set('selfie', $profile->hasSelfie());
$document->set('video', $profile->hasVideo());
$document->set('commented', $profile->isCommented());
$document->set('primaryStationId', $profile->getStations()->count() ? $profile->getStations()->first()->getId() : null);
$stations = [];
foreach($profile->getStations()->toArray() as $k => $v) {
if($k == 0)
continue;
$stations[] = $profile->getStations()->get($k)->getId();
}
$document->set('secondaryStationsIds', $stations);
$document->set('description', $this->translatableValueToAllLanguages($profile->getDescription()));
break;
case self::INDEX_DESCRIPTION:
$text = $profile->getDescription()->getTranslation('ru');
preg_match_all('/\w+/u', $text, $matches);
$words3charsMin = array_filter($matches[0], function($match): bool {
return mb_strlen($match) >= 3;
});
$document->set('descRu', $text);
$document->set('descRu3ChrWords', count($words3charsMin));
$document->set('descRuWords', count($matches[0]));
break;
case self::INDEX_KEY_SEARCH:
$searchKeysData = ''
. $this->getParamString($profile->getPhoneNumber())
. $this->getParamString($this->translatableValueToAllLanguages($profile->getName()))
. $this->getParamString($profile->getPersonParameters()->getAge())
. $this->getParamString($this->translatableValueToAllLanguages($profile->getCity()->getName()))
. $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
$district = $station->getDistrict();
return $district ? $this->translatableValueToAllLanguages($district->getName()) : '';
})->toArray()))
. $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
$county = $station->getCounty();
return $county ? $this->translatableValueToAllLanguages($county->getName()) : '';
})->toArray()))
. $this->getParamString(implode(' ', $profile->getStations()->map(function (Station $station): string {
return $this->translatableValueToAllLanguages($station->getName());
})->toArray()))
. $this->getParamString(implode(' ', $profile->getProvidedServices()->map(function (ProfileService $profileService): string {
return $this->translatableValueToAllLanguages($profileService->getService()->getName());
})->toArray()))
. $this->getParamString($this->translatableValueToAllLanguages($profile->getDescription()));
$searchKeysData = ltrim($searchKeysData, ' ; ');
$document->set('searchKeysData', $searchKeysData);
break;
}
}
}
public static function getSubscribedEvents()
{
return array(
TransformEvent::POST_TRANSFORM => 'postTransformEntity',
);
}
protected function getParamString(string $param): string
{
return ' ; ' . $param;
}
protected function translatableValueToAllLanguages(TranslatableValue $translatableValue): string
{
$str = '';
array_walk($this->siteLanguages, function($language) use (&$str, $translatableValue): void {
$str .= ' ' . $translatableValue->getTranslation($language);
});
return $str;
}
}