src/Aqarmap/Bundle/ListingBundle/EventListener/ListingListener.php line 68

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
  4. use Aqarmap\Bundle\ListingBundle\Constant\ListingCategories;
  5. use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
  6. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  7. use Aqarmap\Bundle\ListingBundle\Service\YoutubeHelper;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. class ListingListener implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var RequestStack
  15.      */
  16.     private $requestStack;
  17.     /**
  18.      * @var YoutubeHelper
  19.      */
  20.     private $youtubeHelper;
  21.     /**
  22.      * @var FeatureToggleManager
  23.      */
  24.     private $featureToggleManager;
  25.     /**
  26.      * @var ListingManager
  27.      */
  28.     private $listingManager;
  29.     /**
  30.      * @var TokenStorage
  31.      */
  32.     private $tokenStorage;
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         YoutubeHelper $youtubeHelper,
  36.         FeatureToggleManager $featureToggleManager,
  37.         ListingManager $listingManager,
  38.         TokenStorageInterface $tokenStorage
  39.     ) {
  40.         $this->requestStack $requestStack;
  41.         $this->youtubeHelper $youtubeHelper;
  42.         $this->featureToggleManager $featureToggleManager;
  43.         $this->listingManager $listingManager;
  44.         $this->tokenStorage $tokenStorage;
  45.     }
  46.     public function preSubmittedListingEvent(ListingEvent $event): void
  47.     {
  48.         $listing $event->getListing();
  49.         // Check if the campaign exsist in the cookie  add it to listing
  50.         $cookies $this->requestStack->getCurrentRequest()->cookies;
  51.         if ($cookies->has('campaign') && null == $listing->getCampaign()) {
  52.             $listing->setCampaign($cookies->get('campaign'));
  53.         }
  54.     }
  55.     // ---------------------------------------------------------------------
  56.     public function preSaveListingEvent(ListingEvent $event): void
  57.     {
  58.         $listing $event->getListing();
  59.         if ($listing->getVideoUrl()) {
  60.             $listing->setVideoUrl(
  61.                 $this->youtubeHelper->getYoutubeShareUrlFromAnyYoutubeUrl(
  62.                     $listing->getVideoUrl()
  63.                 )
  64.             );
  65.         }
  66.         if ($listing->getParent()) {
  67.             $listing->setVideoUrl($listing->getParent()->getVideoUrl());
  68.         }
  69.         $this->listingManager->syncVideoUrl($listing);
  70.         if (null == $listing->getId() && null == $listing->getUser()) {
  71.             $user $this->tokenStorage->getToken()->getUser();
  72.             $listing->setUser($user);
  73.             $listing->setSellerRole($user->getUserType());
  74.         }
  75.         // Assign default admin category for the listing
  76.         if ($listing->getSection() && !$listing->getSection()->getSearchable()) {
  77.             $listing->setCategory(ListingCategories::PROJECTS);
  78.         } elseif (ListingCategories::FIRST_LISTING_FOR_FREE != $listing->getCategory()
  79.             && ListingCategories::SCRAPPED != $listing->getCategory()
  80.             && ListingCategories::UNLIMITED != $listing->getCategory()
  81.         ) {
  82.             $listing->setCategory(ListingCategories::NORMAL);
  83.         }
  84.         // Update updated date
  85.         $listing->setUpdatedAt(new \DateTime());
  86.         // Clear HTML tags from title
  87.         if (null != $listing->getTitle()) {
  88.             $listing->setTitle(strip_tags($listing->getTitle()));
  89.         }
  90.         // Clear HTML tags from description
  91.         if (null != $listing->getDescription()) {
  92.             $listing->setDescription(strip_tags($listing->getDescription()));
  93.         }
  94.         // Clear HTML tags from address
  95.         if (null != $listing->getAddress()) {
  96.             $listing->setAddress(strip_tags($listing->getAddress()));
  97.         }
  98.         if ($listing->getPhones() && $this->featureToggleManager->isEnabled('listing_user_phones')) {
  99.             $this->listingManager->filterListingPhones($listing);
  100.         }
  101.     }
  102.     // ---------------------------------------------------------------------
  103.     public static function getSubscribedEvents()
  104.     {
  105.         return [
  106.             'aqarmap.listing.pre_save' => ['preSaveListingEvent'1],
  107.             'aqarmap.listing.pre_save_listener' => ['preSaveListingEvent'3],
  108.             'aqarmap.listing.pre_submitted' => ['preSubmittedListingEvent'2],
  109.         ];
  110.     }
  111. }