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

  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use App\Service\User\SubAccountService;
  4. use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
  5. use Aqarmap\Bundle\ListingBundle\Constant\ListingCategories;
  6. use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
  7. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  8. use Aqarmap\Bundle\ListingBundle\Service\YoutubeHelper;
  9. use Aqarmap\Bundle\UserBundle\Entity\User;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. class ListingListener implements EventSubscriberInterface
  14. {
  15. /**
  16. * @var TokenStorage
  17. */
  18. private $tokenStorage;
  19. public function __construct(
  20. private readonly RequestStack $requestStack,
  21. private readonly YoutubeHelper $youtubeHelper,
  22. private readonly FeatureToggleManager $featureToggleManager,
  23. private readonly ListingManager $listingManager,
  24. private readonly SubAccountService $subAccountService,
  25. TokenStorageInterface $tokenStorage,
  26. ) {
  27. $this->tokenStorage = $tokenStorage;
  28. }
  29. public function preSubmittedListingEvent(ListingEvent $event): void
  30. {
  31. $listing = $event->getListing();
  32. // Check if the campaign exsist in the cookie add it to listing
  33. $cookies = $this->requestStack->getCurrentRequest()->cookies;
  34. if ($cookies->has('campaign') && null == $listing->getCampaign()) {
  35. $listing->setCampaign($cookies->get('campaign'));
  36. }
  37. }
  38. // ---------------------------------------------------------------------
  39. public function preSaveListingEvent(ListingEvent $event): void
  40. {
  41. $listing = $event->getListing();
  42. if ($listing->getVideoUrl()) {
  43. $listing->setVideoUrl(
  44. $this->youtubeHelper->getYoutubeShareUrlFromAnyYoutubeUrl(
  45. $listing->getVideoUrl()
  46. )
  47. );
  48. }
  49. if ($listing->getParent()) {
  50. $listing->setVideoUrl($listing->getParent()->getVideoUrl());
  51. }
  52. if ($listing->getId()) {
  53. $this->listingManager->syncVideoUrl($listing);
  54. }
  55. if (null == $listing->getId() && null == $listing->getUser()) {
  56. $user = $this->tokenStorage->getToken()->getUser();
  57. // Ownership is centralised under the main account: a sub account allowed to manage
  58. // listings publishes into its parent's portfolio, so the parent owns the listing (and
  59. // pays for it) while the sub account is recorded as the publisher.
  60. $owner = $user instanceof User ? $this->subAccountService->resolveListingOwner($user) : $user;
  61. $listing->setPublisher($user instanceof User ? $user : null);
  62. $listing->setUser($owner);
  63. // Keyed to the owner, matching ListingRepository::updateSellerRole(), which rewrites the
  64. // seller role of every listing owned by a user when an admin changes their user type.
  65. $listing->setSellerRole($owner->getUserType());
  66. }
  67. // Assign default admin category for the listing
  68. if ($listing->getSection() && !$listing->getSection()->getSearchable()) {
  69. $listing->setCategory(ListingCategories::PROJECTS);
  70. } elseif (ListingCategories::FIRST_LISTING_FOR_FREE != $listing->getCategory()
  71. && ListingCategories::SCRAPPED != $listing->getCategory()
  72. && ListingCategories::UNLIMITED != $listing->getCategory()
  73. ) {
  74. $listing->setCategory(ListingCategories::NORMAL);
  75. }
  76. // Update updated date
  77. $listing->setUpdatedAt(new \DateTime());
  78. // Clear HTML tags from title
  79. if (null != $listing->getTitle()) {
  80. $listing->setTitle(strip_tags($listing->getTitle()));
  81. }
  82. // Clear HTML tags from description
  83. if (null != $listing->getDescription()) {
  84. $listing->setDescription(strip_tags($listing->getDescription()));
  85. }
  86. // Clear HTML tags from address
  87. if (null != $listing->getAddress()) {
  88. $listing->setAddress(strip_tags($listing->getAddress()));
  89. }
  90. if ($listing->getPhones() && $this->featureToggleManager->isEnabled('listing_user_phones')) {
  91. $this->listingManager->filterListingPhones($listing);
  92. }
  93. }
  94. // ---------------------------------------------------------------------
  95. public static function getSubscribedEvents(): array
  96. {
  97. return [
  98. 'aqarmap.listing.pre_save' => ['preSaveListingEvent', 1],
  99. 'aqarmap.listing.pre_save_listener' => ['preSaveListingEvent', 3],
  100. 'aqarmap.listing.pre_submitted' => ['preSubmittedListingEvent', 2],
  101. ];
  102. }
  103. }