src/Aqarmap/Bundle/ListingBundle/EventListener/ListingFeatureListener.php line 76

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\ListingBundle\EventListener;
  3. use Aqarmap\Bundle\ListingBundle\Constant\ListingFeatures;
  4. use Aqarmap\Bundle\ListingBundle\Entity\ListingFeature;
  5. use Aqarmap\Bundle\ListingBundle\Event\ListingFeatureEvent;
  6. use Aqarmap\Bundle\ListingBundle\Model\Contracts\BumpUpModelInterface;
  7. use Aqarmap\Bundle\ListingBundle\Service\CallLog\Logger;
  8. use Aqarmap\Bundle\ListingBundle\Service\Contracts\ListingFeatureServiceInterface;
  9. use Aqarmap\Bundle\ListingBundle\Service\ListingManager;
  10. use Aqarmap\Bundle\MainBundle\Constant\ActivityType;
  11. use Aqarmap\Bundle\MainBundle\Service\ActivityLogger;
  12. use Aqarmap\Bundle\MainBundle\Service\Setting;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. class ListingFeatureListener implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var ListingFeatureServiceInterface
  19.      */
  20.     private $listingFeatureService;
  21.     /**
  22.      * @var BumpUpModelInterface
  23.      */
  24.     private $bumpUpModel;
  25.     /**
  26.      * @var Setting
  27.      */
  28.     private $settings;
  29.     /**
  30.      * @var TokenStorageInterface
  31.      */
  32.     private $tokenStorage;
  33.     /**
  34.      * @var ListingManager
  35.      */
  36.     private $listingManager;
  37.     /**
  38.      * @var ActivityLogger
  39.      */
  40.     private $activityLogger;
  41.     private $user;
  42.     /**
  43.      * @var Logger
  44.      */
  45.     private $logger;
  46.     public function __construct(
  47.         ListingFeatureServiceInterface $listingFeatureService,
  48.         BumpUpModelInterface $bumpUpModel,
  49.         Setting $settings,
  50.         TokenStorageInterface $tokenStorage,
  51.         ListingManager $listingManager,
  52.         ActivityLogger $activityLogger,
  53.         Logger $logger
  54.     ) {
  55.         $this->listingFeatureService $listingFeatureService;
  56.         $this->bumpUpModel $bumpUpModel;
  57.         $this->settings $settings;
  58.         $this->tokenStorage $tokenStorage;
  59.         $this->listingManager $listingManager;
  60.         $this->activityLogger $activityLogger;
  61.         $this->logger $logger;
  62.         $this->user $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  63.     }
  64.     public function onFeaturingCreateAutoBumpUp(ListingFeatureEvent $event): void
  65.     {
  66.         $listingFeature $event->getListingFeature();
  67.         if (\in_array($listingFeature->getType(), ListingFeatures::getBumpUpFeatures())) {
  68.             try {
  69.                 $listing $listingFeature->getListing();
  70.                 $listing->setBumped(true);
  71.                 $bumpUpModel $this->createBumpUpModel($listingFeature);
  72.                 $this->listingFeatureService->createBumpUp($bumpUpModel);
  73.                 $this->listingManager->saveListing($listingFeature->getListing(), true);
  74.                 $this->activityLogger->record(ActivityType::LISTING_BUMP_UP$listing->getId());
  75.                 $this->listingFeatureService->logBumpActivity('aqarmap.listing.bump_up_started'$listing$this->user);
  76.             } catch (\Exception $e) {
  77.                 $this->logger->error($e->getMessage());
  78.             }
  79.         }
  80.     }
  81.     /**
  82.      * {@inheri tdoc}.
  83.      */
  84.     public static function getSubscribedEvents()
  85.     {
  86.         return [
  87.             'aqarmap.listing.feature.bumpup' => 'onFeaturingCreateAutoBumpUp',
  88.         ];
  89.     }
  90.     private function createBumpUpModel(ListingFeature $listingFeature): BumpUpModelInterface
  91.     {
  92.         $featuringOccurenceInterval $this->getFeaturingOccurenceInterval($listingFeature);
  93.         $durationInDays date_diff(new \DateTime(), $listingFeature->getExpiresAt())->format('%a');
  94.         $occurrences $featuringOccurenceInterval round($durationInDays $featuringOccurenceInterval) : 0;
  95.         return $this->bumpUpModel
  96.             ->setListing($listingFeature->getListing())
  97.             ->setDuration($durationInDays)
  98.             ->setOccurrences($occurrences)
  99.             ->setOwner($this->user)
  100.         ;
  101.     }
  102.     private function getFeaturingOccurenceInterval(ListingFeature $listingFeature)
  103.     {
  104.         switch ($listingFeature->getType()) {
  105.             case ListingFeatures::SPONSORED:
  106.                 $Interval $this->settings->getSetting('general''auto_bumpup_sponserd_interval');
  107.                 break;
  108.             case ListingFeatures::SPOTLIGHT:
  109.                 $Interval $this->settings->getSetting('general''auto_bumpup_spotlight_interval');
  110.                 break;
  111.         }
  112.         return $Interval;
  113.     }
  114. }