<?php
namespace Aqarmap\Bundle\ListingBundle\EventListener;
use Aqarmap\Bundle\ListingBundle\Constant\ListingFeatures;
use Aqarmap\Bundle\ListingBundle\Entity\ListingFeature;
use Aqarmap\Bundle\ListingBundle\Event\ListingFeatureEvent;
use Aqarmap\Bundle\ListingBundle\Model\Contracts\BumpUpModelInterface;
use Aqarmap\Bundle\ListingBundle\Service\CallLog\Logger;
use Aqarmap\Bundle\ListingBundle\Service\Contracts\ListingFeatureServiceInterface;
use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
use Aqarmap\Bundle\MainBundle\Constant\ActivityType;
use Aqarmap\Bundle\MainBundle\Service\ActivityLogger;
use Aqarmap\Bundle\MainBundle\Service\Setting;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class ListingFeatureListener implements EventSubscriberInterface
{
/**
* @var ListingFeatureServiceInterface
*/
private $listingFeatureService;
/**
* @var BumpUpModelInterface
*/
private $bumpUpModel;
/**
* @var Setting
*/
private $settings;
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var ListingManager
*/
private $listingManager;
/**
* @var ActivityLogger
*/
private $activityLogger;
private $user;
/**
* @var Logger
*/
private $logger;
public function __construct(
ListingFeatureServiceInterface $listingFeatureService,
BumpUpModelInterface $bumpUpModel,
Setting $settings,
TokenStorageInterface $tokenStorage,
ListingManager $listingManager,
ActivityLogger $activityLogger,
Logger $logger
) {
$this->listingFeatureService = $listingFeatureService;
$this->bumpUpModel = $bumpUpModel;
$this->settings = $settings;
$this->tokenStorage = $tokenStorage;
$this->listingManager = $listingManager;
$this->activityLogger = $activityLogger;
$this->logger = $logger;
$this->user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
}
public function onFeaturingCreateAutoBumpUp(ListingFeatureEvent $event): void
{
$listingFeature = $event->getListingFeature();
if (\in_array($listingFeature->getType(), ListingFeatures::getBumpUpFeatures())) {
try {
if ($listingFeature->getExpiresAt()) {
$listing = $listingFeature->getListing();
$listing->setBumped(true);
$bumpUpModel = $this->createBumpUpModel($listingFeature);
$this->listingFeatureService->createBumpUp($bumpUpModel);
$this->listingManager->saveListing($listingFeature->getListing(), true);
$this->activityLogger->record(ActivityType::LISTING_BUMP_UP, $listing->getId());
$this->listingFeatureService->logBumpActivity('aqarmap.listing.bump_up_started', $listing, $this->user);
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}
/**
* {@inheri tdoc}.
*/
public static function getSubscribedEvents()
{
return [
'aqarmap.listing.feature.bumpup' => 'onFeaturingCreateAutoBumpUp',
];
}
private function createBumpUpModel(ListingFeature $listingFeature): BumpUpModelInterface
{
$featuringOccurenceInterval = $this->getFeaturingOccurenceInterval($listingFeature);
$durationInDays = date_diff(new \DateTime(), $listingFeature->getExpiresAt())->format('%a');
$occurrences = $featuringOccurenceInterval > 0 ? round($durationInDays / $featuringOccurenceInterval) : 0;
return $this->bumpUpModel
->setListing($listingFeature->getListing())
->setDuration($durationInDays)
->setOccurrences($occurrences)
->setOwner($this->user)
;
}
private function getFeaturingOccurenceInterval(ListingFeature $listingFeature)
{
switch ($listingFeature->getType()) {
case ListingFeatures::SPONSORED:
$Interval = $this->settings->getSetting('general', 'auto_bumpup_sponserd_interval');
break;
case ListingFeatures::SPOTLIGHT:
$Interval = $this->settings->getSetting('general', 'auto_bumpup_spotlight_interval');
break;
}
return $Interval;
}
}