<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\FeatureToggleBundle\Service\FeatureToggleManager;
use Aqarmap\Bundle\ListingBundle\Constant\ListingCategories;
use Aqarmap\Bundle\ListingBundle\Event\ListingEvent;
use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
use Aqarmap\Bundle\ListingBundle\Service\YoutubeHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ListingListener implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var YoutubeHelper
*/
private $youtubeHelper;
/**
* @var FeatureToggleManager
*/
private $featureToggleManager;
/**
* @var ListingManager
*/
private $listingManager;
/**
* @var TokenStorage
*/
private $tokenStorage;
public function __construct(
RequestStack $requestStack,
YoutubeHelper $youtubeHelper,
FeatureToggleManager $featureToggleManager,
ListingManager $listingManager,
TokenStorageInterface $tokenStorage
) {
$this->requestStack = $requestStack;
$this->youtubeHelper = $youtubeHelper;
$this->featureToggleManager = $featureToggleManager;
$this->listingManager = $listingManager;
$this->tokenStorage = $tokenStorage;
}
public function preSubmittedListingEvent(ListingEvent $event): void
{
$listing = $event->getListing();
// Check if the campaign exsist in the cookie add it to listing
$cookies = $this->requestStack->getCurrentRequest()->cookies;
if ($cookies->has('campaign') && null == $listing->getCampaign()) {
$listing->setCampaign($cookies->get('campaign'));
}
}
// ---------------------------------------------------------------------
public function preSaveListingEvent(ListingEvent $event): void
{
$listing = $event->getListing();
if ($listing->getVideoUrl()) {
$listing->setVideoUrl(
$this->youtubeHelper->getYoutubeShareUrlFromAnyYoutubeUrl(
$listing->getVideoUrl()
)
);
}
if ($listing->getParent()) {
$listing->setVideoUrl($listing->getParent()->getVideoUrl());
}
$this->listingManager->syncVideoUrl($listing);
if (null == $listing->getId() && null == $listing->getUser()) {
$user = $this->tokenStorage->getToken()->getUser();
$listing->setUser($user);
$listing->setSellerRole($user->getUserType());
}
// Assign default admin category for the listing
if ($listing->getSection() && !$listing->getSection()->getSearchable()) {
$listing->setCategory(ListingCategories::PROJECTS);
} elseif (ListingCategories::FIRST_LISTING_FOR_FREE != $listing->getCategory()
&& ListingCategories::SCRAPPED != $listing->getCategory()
&& ListingCategories::UNLIMITED != $listing->getCategory()
) {
$listing->setCategory(ListingCategories::NORMAL);
}
// Update updated date
$listing->setUpdatedAt(new \DateTime());
// Clear HTML tags from title
if (null != $listing->getTitle()) {
$listing->setTitle(strip_tags($listing->getTitle()));
}
// Clear HTML tags from description
if (null != $listing->getDescription()) {
$listing->setDescription(strip_tags($listing->getDescription()));
}
// Clear HTML tags from address
if (null != $listing->getAddress()) {
$listing->setAddress(strip_tags($listing->getAddress()));
}
if ($listing->getPhones() && $this->featureToggleManager->isEnabled('listing_user_phones')) {
$this->listingManager->filterListingPhones($listing);
}
}
// ---------------------------------------------------------------------
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.pre_save' => ['preSaveListingEvent', 1],
'aqarmap.listing.pre_save_listener' => ['preSaveListingEvent', 3],
'aqarmap.listing.pre_submitted' => ['preSubmittedListingEvent', 2],
];
}
}