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

  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. private $user;
  18. public function __construct(
  19. private readonly ListingFeatureServiceInterface $listingFeatureService,
  20. private readonly BumpUpModelInterface $bumpUpModel,
  21. private readonly Setting $settings,
  22. private readonly TokenStorageInterface $tokenStorage,
  23. private readonly ListingManager $listingManager,
  24. private readonly ActivityLogger $activityLogger,
  25. private readonly Logger $logger,
  26. ) {
  27. $this->user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
  28. }
  29. public function onFeaturingCreateAutoBumpUp(ListingFeatureEvent $event): void
  30. {
  31. $listingFeature = $event->getListingFeature();
  32. if (\in_array($listingFeature->getType(), ListingFeatures::getBumpUpFeatures())) {
  33. try {
  34. if ($listingFeature->getExpiresAt()) {
  35. $listing = $listingFeature->getListing();
  36. $listing->setBumped(true);
  37. $bumpUpModel = $this->createBumpUpModel($listingFeature);
  38. $this->listingFeatureService->createBumpUp($bumpUpModel);
  39. $this->listingManager->saveListing($listingFeature->getListing(), true);
  40. $this->activityLogger->record(ActivityType::LISTING_BUMP_UP, $listing->getId());
  41. $this->listingFeatureService->logBumpActivity('aqarmap.listing.bump_up_started', $listing, $this->user);
  42. }
  43. } catch (\Exception $e) {
  44. $this->logger->error($e->getMessage());
  45. }
  46. }
  47. }
  48. /**
  49. * {@inheri tdoc}.
  50. */
  51. public static function getSubscribedEvents(): array
  52. {
  53. return [
  54. 'aqarmap.listing.feature.bumpup' => 'onFeaturingCreateAutoBumpUp',
  55. ];
  56. }
  57. private function createBumpUpModel(ListingFeature $listingFeature): BumpUpModelInterface
  58. {
  59. $featuringOccurenceInterval = $this->getFeaturingOccurenceInterval($listingFeature);
  60. $durationInDays = date_diff(new \DateTime(), $listingFeature->getExpiresAt())->format('%a');
  61. $occurrences = $featuringOccurenceInterval > 0 ? round($durationInDays / $featuringOccurenceInterval) : 0;
  62. return $this->bumpUpModel
  63. ->setListing($listingFeature->getListing())
  64. ->setDuration($durationInDays)
  65. ->setOccurrences($occurrences)
  66. ->setOwner($this->user)
  67. ;
  68. }
  69. private function getFeaturingOccurenceInterval(ListingFeature $listingFeature)
  70. {
  71. return match ($listingFeature->getType()) {
  72. ListingFeatures::SPONSORED => $this->settings->getSetting('general', 'auto_bumpup_sponserd_interval'),
  73. ListingFeatures::SPOTLIGHT => $this->settings->getSetting('general', 'auto_bumpup_spotlight_interval'),
  74. default => $Interval,
  75. };
  76. }
  77. }