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.                 if ($listingFeature->getExpiresAt()) {
  70.                     $listing $listingFeature->getListing();
  71.                     $listing->setBumped(true);
  72.                     $bumpUpModel $this->createBumpUpModel($listingFeature);
  73.                     $this->listingFeatureService->createBumpUp($bumpUpModel);
  74.                     $this->listingManager->saveListing($listingFeature->getListing(), true);
  75.                     $this->activityLogger->record(ActivityType::LISTING_BUMP_UP$listing->getId());
  76.                     $this->listingFeatureService->logBumpActivity('aqarmap.listing.bump_up_started'$listing$this->user);
  77.                 }
  78.             } catch (\Exception $e) {
  79.                 $this->logger->error($e->getMessage());
  80.             }
  81.         }
  82.     }
  83.     /**
  84.      * {@inheri tdoc}.
  85.      */
  86.     public static function getSubscribedEvents()
  87.     {
  88.         return [
  89.             'aqarmap.listing.feature.bumpup' => 'onFeaturingCreateAutoBumpUp',
  90.         ];
  91.     }
  92.     private function createBumpUpModel(ListingFeature $listingFeature): BumpUpModelInterface
  93.     {
  94.         $featuringOccurenceInterval $this->getFeaturingOccurenceInterval($listingFeature);
  95.         $durationInDays date_diff(new \DateTime(), $listingFeature->getExpiresAt())->format('%a');
  96.         $occurrences $featuringOccurenceInterval round($durationInDays $featuringOccurenceInterval) : 0;
  97.         return $this->bumpUpModel
  98.             ->setListing($listingFeature->getListing())
  99.             ->setDuration($durationInDays)
  100.             ->setOccurrences($occurrences)
  101.             ->setOwner($this->user)
  102.         ;
  103.     }
  104.     private function getFeaturingOccurenceInterval(ListingFeature $listingFeature)
  105.     {
  106.         switch ($listingFeature->getType()) {
  107.             case ListingFeatures::SPONSORED:
  108.                 $Interval $this->settings->getSetting('general''auto_bumpup_sponserd_interval');
  109.                 break;
  110.             case ListingFeatures::SPOTLIGHT:
  111.                 $Interval $this->settings->getSetting('general''auto_bumpup_spotlight_interval');
  112.                 break;
  113.         }
  114.         return $Interval;
  115.     }
  116. }