src/Aqarmap/Bundle/ListingBundle/EventListener/ListingPreSerializerListener.php line 142

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\FinancialAidsBundle\Service\FinancialAidService;
  4. use Aqarmap\Bundle\ListingBundle\Constant\EligibleForMortgageData;
  5. use Aqarmap\Bundle\ListingBundle\Constant\ListingCustomFields;
  6. use Aqarmap\Bundle\ListingBundle\Constant\PropertyRegistrationStatusOption;
  7. use Aqarmap\Bundle\ListingBundle\Entity\Listing;
  8. use Aqarmap\Bundle\ListingBundle\Entity\Photo;
  9. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  10. use Aqarmap\Bundle\ListingBundle\Service\V4\CompoundDetailService;
  11. use Aqarmap\Bundle\ListingBundle\Service\V4\CostPerLeadService;
  12. use Aqarmap\Bundle\MainBundle\Service\ThumbURL;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Gedmo\Translatable\TranslatableListener;
  15. use JMS\Serializer\EventDispatcher\Events;
  16. use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
  17. use JMS\Serializer\EventDispatcher\PreSerializeEvent;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
  21. /**
  22.  * Add thumbs url pre serialization.
  23.  */
  24. class ListingPreSerializerListener implements EventSubscriberInterface
  25. {
  26.     public const SIMILAR_LISTINGS_LIMIT 10;
  27.     private $uploaderHelper;
  28.     private $thumbURL;
  29.     private $translator;
  30.     private $request;
  31.     private $locales;
  32.     private $entityManager;
  33.     private $translatableListener;
  34.     /** @var CompoundDetailService */
  35.     private $compoundDetailService;
  36.     /** @var FinancialAidService */
  37.     private $financialAidService;
  38.     /** @var CostPerLeadService */
  39.     private $costPerLeadService;
  40.     public function __construct(
  41.         ThumbURL $thumbURL,
  42.         UploaderHelper $uploaderHelper,
  43.         TranslatorInterface $translator,
  44.         RequestStack $request,
  45.         array $locales,
  46.         EntityManagerInterface $entityManager,
  47.         TranslatableListener $translatableListener,
  48.         CompoundDetailService $compoundDetailService,
  49.         FinancialAidService $financialAidService,
  50.         ListingManager $listingManager,
  51.         CostPerLeadService $costPerLeadService
  52.     ) {
  53.         $this->thumbURL $thumbURL;
  54.         $this->uploaderHelper $uploaderHelper;
  55.         $this->translator $translator;
  56.         $this->request $request->getCurrentRequest();
  57.         if ($this->request) {
  58.             $this->translator->setLocale($this->request->getLocale());
  59.         }
  60.         $this->locales $locales;
  61.         $this->entityManager $entityManager;
  62.         $this->translatableListener $translatableListener;
  63.         $this->compoundDetailService $compoundDetailService;
  64.         $this->financialAidService $financialAidService;
  65.         $this->costPerLeadService $costPerLeadService;
  66.     }
  67.     public static function getSubscribedEvents()
  68.     {
  69.         return [
  70.             [
  71.                 'event' => Events::PRE_SERIALIZE,
  72.                 'class' => Listing::class,
  73.                 'method' => 'setLogos',
  74.             ],
  75.             [
  76.                 'event' => Events::PRE_SERIALIZE,
  77.                 'class' => Listing::class,
  78.                 'method' => 'translateLabels',
  79.             ],
  80.             [
  81.                 'event' => Events::PRE_SERIALIZE,
  82.                 'class' => Listing::class,
  83.                 'method' => 'translateListingV4',
  84.             ],
  85.             [
  86.                 'event' => Events::PRE_SERIALIZE,
  87.                 'class' => Listing::class,
  88.                 'method' => 'setPropertyTypChildren',
  89.             ],
  90.             [
  91.                 'event' => Events::PRE_SERIALIZE,
  92.                 'class' => Listing::class,
  93.                 'method' => 'setCostPerLead',
  94.             ],
  95.         ];
  96.     }
  97.     public function setLogos(PreSerializeEvent $event): void
  98.     {
  99.         // if not in group serialization, skip
  100.         foreach (['ProjectSearchV4''ProjectDetailsV4''locationListingV2'] as $group) {
  101.             if ($this->hasGroup($event$group)) {
  102.                 break;
  103.             }
  104.             return;
  105.         }
  106.         /** @var Listing $listing */
  107.         $listing $event->getObject();
  108.         if ($listing->getLogo()) {
  109.             $listing->setListingLogo($this->thumbURL->generateURL($this->uploaderHelper->asset($listing->getLogo()->getFile(), 'file'), 'logo'));
  110.         }
  111.         if ($listing->getValidUserLogo()) {
  112.             $listing->setUserLogo($this->thumbURL->generateURL($this->uploaderHelper->asset($listing->getValidUserLogo(), 'file'), 'logo'));
  113.         }
  114.     }
  115.     /**
  116.      * Set PropertyTypeChildren (listings) of compounds.
  117.      */
  118.     public function setPropertyTypChildren(PreSerializeEvent $event): void
  119.     {
  120.         /** @var Listing $listing */
  121.         $listing $event->getObject();
  122.         if ($this->hasGroup($event'ProjectDetailsV4')) {
  123.             $this->compoundDetailService->getPropertyTypeChildrens($listing$this->request->getLocale());
  124.         }
  125.     }
  126.     public function translateLabels(PreSerializeEvent $event): void
  127.     {
  128.         /** @var Listing $listing */
  129.         $listing $event->getObject();
  130.         if ($listing->hasPropertyRegistrationStatusOptions()) {
  131.             $listing->setPropertyRegistrationStatusOptions($this->getPropertyRegistrationStatusTranslatedOptions());
  132.         } else {
  133.             $listing->setPropertyRegistrationStatusOptions([]);
  134.         }
  135.         if (!empty($listing->getEligibleForMortgage())) {
  136.             $listing->setEligibleForMortgageData($this->getEligibleForMortgageTranslated($listing));
  137.         } else {
  138.             $listing->setEligibleForMortgageData([]);
  139.         }
  140.         $attributesList $listing->getListingAttributesList() ?: [];
  141.         $translatedAttributesList = [];
  142.         foreach ($attributesList as $key => $value) {
  143.             if (ListingCustomFields::FINISH_TYPE_LABEL_VALUE == $key) {
  144.                 $translatedAttributesList[$key] = $this->translator->trans($value);
  145.                 continue;
  146.             }
  147.             $translatedAttributesList[$key] = $value;
  148.         }
  149.         $listing->setListingAttributesList($translatedAttributesList);
  150.         $mappedPhotos $listing->getMappedPhotos() ?: [];
  151.         foreach ($mappedPhotos as $key => $value) {
  152.             if (isset($mappedPhotos[$key]['photo']) && $mappedPhotos[$key]['photo'] instanceof Photo) {
  153.                 $mappedPhotos[$key]['thumbnails'] = [
  154.                     'large' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mappedPhotos[$key]['photo'], 'file'), 'large'),
  155.                     'small' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mappedPhotos[$key]['photo'], 'file'), 'small'),
  156.                     'thumb' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mappedPhotos[$key]['photo'], 'file'), 'search-thumb-webp'),
  157.                 ];
  158.                 unset($mappedPhotos[$key]['photo']);
  159.             }
  160.         }
  161.         $listing->setMappedPhotos($mappedPhotos);
  162.         $mainPhoto $listing->getMappedMainPhoto() ?: null;
  163.         if (!empty($mainPhoto) && isset($mainPhoto['photo']) && $mainPhoto['photo'] instanceof Photo) {
  164.             $mainPhoto['thumbnails'] = [
  165.                 'large' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mainPhoto['photo'], 'file'), 'large'),
  166.                 'small' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mainPhoto['photo'], 'file'), 'small'),
  167.                 'thumb' => $this->thumbURL->generateURL($this->uploaderHelper->asset($mainPhoto['photo'], 'file'), 'search-thumb-webp'),
  168.             ];
  169.             unset($mainPhoto['photo']);
  170.         }
  171.         $listing->setMappedMainPhoto($mainPhoto);
  172.     }
  173.     public function translateListingV4(PreSerializeEvent $event): void
  174.     {
  175.         if ($this->hasGroup($event'DefaultV4') || $this->hasGroup($event'SearchV4') || $this->hasGroup($event'MyListing')) {
  176.             /** @var Listing $listing */
  177.             $listing $event->getObject();
  178.             $translations $listing->getV4Translations() ?: [];
  179.             $index 0;
  180.             if ($listing->getTranslations()->count() || $listing->getTitle() || $listing->getDescription()) {
  181.                 $this->translatableListener->setTranslationFallback(false);
  182.                 foreach ($this->locales as $locale) {
  183.                     $listing->setTranslatableLocale($locale);
  184.                     $this->entityManager->refresh($listing);
  185.                     if ($listing->getTitle()) {
  186.                         $translations[$index]['locale'] = $locale;
  187.                         $translations[$index]['title'] = $listing->getTitle();
  188.                     }
  189.                     if ($listing->getDescription()) {
  190.                         $translations[$index]['locale'] = $locale;
  191.                         $translations[$index]['description'] = $listing->getDescription();
  192.                     }
  193.                     ++$index;
  194.                 }
  195.             }
  196.             $this->translatableListener->setTranslationFallback(true);
  197.             if ($this->request) {
  198.                 $listing->setTranslatableLocale($this->request->getLocale());
  199.             }
  200.             $this->entityManager->refresh($listing);
  201.             $listing->setV4Translations($translations);
  202.         }
  203.     }
  204.     /**
  205.      * Set cost per lead to listing.
  206.      */
  207.     public function setCostPerLead(PreSerializeEvent $event): void
  208.     {
  209.         /** @var Listing $listing */
  210.         $listing $event->getObject();
  211.         $costPerLead $this->costPerLeadService->getCostPerLeadByListing($listing);
  212.         $listing->setCostPerLead($costPerLead);
  213.     }
  214.     private function hasGroup(PreSerializeEvent $eventstring $group): bool
  215.     {
  216.         try {
  217.             $groups $event->getContext()->getAttribute('groups');
  218.         } catch (\Exception $exception) {
  219.             return false;
  220.         }
  221.         return $groups && \in_array($group$groups);
  222.     }
  223.     /**
  224.      * Get Mortgage Translated Options.
  225.      *
  226.      * @return array
  227.      */
  228.     private function getPropertyRegistrationStatusTranslatedOptions()
  229.     {
  230.         foreach (PropertyRegistrationStatusOption::getChoices() as $key => $status) {
  231.             $statusOptions[] = [
  232.                 'id' => $key,
  233.                 'title' => $this->translator->trans($status),
  234.             ];
  235.         }
  236.         return $statusOptions;
  237.     }
  238.     /**
  239.      * Get Eligible For Mortgage Translated.
  240.      *
  241.      * @return array
  242.      */
  243.     private function getEligibleForMortgageTranslated(Listing $listing)
  244.     {
  245.         foreach ($listing->getEligibleForMortgage() as $mortgage) {
  246.             $eligibleForMortgageTranslated[] = [
  247.                 'id' => $mortgage,
  248.                 'title' => $this->translator->trans(EligibleForMortgageData::getLabel($mortgage)),
  249.             ];
  250.         }
  251.         return $eligibleForMortgageTranslated;
  252.     }
  253.     /**
  254.      * set FinancialAid in listing for sale.
  255.      *
  256.      * @throws \Doctrine\ORM\NonUniqueResultException
  257.      */
  258.     private function setFinancialAid(Listing $listing): void
  259.     {
  260.         $this->financialAidService->setFinancialAidInListing($listing);
  261.     }
  262. }