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

  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 TokenStorage
  15. */
  16. private $tokenStorage;
  17. public function __construct(
  18. private readonly RequestStack $requestStack,
  19. private readonly YoutubeHelper $youtubeHelper,
  20. private readonly FeatureToggleManager $featureToggleManager,
  21. private readonly ListingManager $listingManager,
  22. TokenStorageInterface $tokenStorage,
  23. ) {
  24. $this->tokenStorage = $tokenStorage;
  25. }
  26. public function preSubmittedListingEvent(ListingEvent $event): void
  27. {
  28. $listing = $event->getListing();
  29. // Check if the campaign exsist in the cookie add it to listing
  30. $cookies = $this->requestStack->getCurrentRequest()->cookies;
  31. if ($cookies->has('campaign') && null == $listing->getCampaign()) {
  32. $listing->setCampaign($cookies->get('campaign'));
  33. }
  34. }
  35. // ---------------------------------------------------------------------
  36. public function preSaveListingEvent(ListingEvent $event): void
  37. {
  38. $listing = $event->getListing();
  39. if ($listing->getVideoUrl()) {
  40. $listing->setVideoUrl(
  41. $this->youtubeHelper->getYoutubeShareUrlFromAnyYoutubeUrl(
  42. $listing->getVideoUrl()
  43. )
  44. );
  45. }
  46. if ($listing->getParent()) {
  47. $listing->setVideoUrl($listing->getParent()->getVideoUrl());
  48. }
  49. $this->listingManager->syncVideoUrl($listing);
  50. if (null == $listing->getId() && null == $listing->getUser()) {
  51. $user = $this->tokenStorage->getToken()->getUser();
  52. $listing->setUser($user);
  53. $listing->setSellerRole($user->getUserType());
  54. }
  55. // Assign default admin category for the listing
  56. if ($listing->getSection() && !$listing->getSection()->getSearchable()) {
  57. $listing->setCategory(ListingCategories::PROJECTS);
  58. } elseif (ListingCategories::FIRST_LISTING_FOR_FREE != $listing->getCategory()
  59. && ListingCategories::SCRAPPED != $listing->getCategory()
  60. && ListingCategories::UNLIMITED != $listing->getCategory()
  61. ) {
  62. $listing->setCategory(ListingCategories::NORMAL);
  63. }
  64. // Update updated date
  65. $listing->setUpdatedAt(new \DateTime());
  66. // Clear HTML tags from title
  67. if (null != $listing->getTitle()) {
  68. $listing->setTitle(strip_tags($listing->getTitle()));
  69. }
  70. // Clear HTML tags from description
  71. if (null != $listing->getDescription()) {
  72. $listing->setDescription(strip_tags($listing->getDescription()));
  73. }
  74. // Clear HTML tags from address
  75. if (null != $listing->getAddress()) {
  76. $listing->setAddress(strip_tags($listing->getAddress()));
  77. }
  78. if ($listing->getPhones() && $this->featureToggleManager->isEnabled('listing_user_phones')) {
  79. $this->listingManager->filterListingPhones($listing);
  80. }
  81. }
  82. // ---------------------------------------------------------------------
  83. public static function getSubscribedEvents(): array
  84. {
  85. return [
  86. 'aqarmap.listing.pre_save' => ['preSaveListingEvent', 1],
  87. 'aqarmap.listing.pre_save_listener' => ['preSaveListingEvent', 3],
  88. 'aqarmap.listing.pre_submitted' => ['preSubmittedListingEvent', 2],
  89. ];
  90. }
  91. }