src/Service/ProfileTopBoard.php line 110

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Dto\ProfileListSpecification;
  4. use App\Entity\Location\City;
  5. use App\Entity\Profile\Profile;
  6. use App\Entity\Sales\Profile\PlacementCharge;
  7. use App\Entity\Sales\Profile\TopPlacement;
  8. use App\Event\Profile\ProfilesShownEvent;
  9. use App\Event\Profile\ProfileWasPlacedOnTop;
  10. use App\Repository\PaidPlacementPriceRepository;
  11. use App\Repository\ProfileTopPlacementRepository;
  12. use Carbon\CarbonImmutable;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Doctrine\Persistence\ObjectManager;
  16. use Happyr\DoctrineSpecification\Filter\Filter;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. class ProfileTopBoard
  19. {
  20.     private ?ObjectManager $entityManager;
  21.     public function __construct(
  22.         ManagerRegistry $managerRegistry,
  23.         private ProfileChargesCalculator $profileChargesCalculator,
  24.         private PaidPlacementPriceRepository  $paidPlacementPriceRepository,
  25.         private Features $features,
  26.         private AccountFinances $accountFinances,
  27.         private ProfileTopPlacementRepository $profileTopPlacementRepository,
  28.         private EventDispatcherInterface $eventDispatcher,
  29.         private CurrentCityResolver $cityResolver,
  30.         private ProfileAdBoard $profileAdBoard
  31.     )
  32.     {
  33.         $this->entityManager $managerRegistry->getManagerForClass(TopPlacement::class);
  34.     }
  35.     public function doPlaceOnTop(Profile $profile\DateTimeImmutable $placedAt\DateTimeImmutable $placedUntil): void
  36.     {
  37.         $city $profile->getCity();
  38.         $timezone $placedAt->getTimezone();
  39.         $now = new \DateTime('now'$timezone);
  40.         if($placedAt->format("Y-m-d H") == $now->format("Y-m-d H")) {
  41.             throw new \LogicException('Нельзя разместить на текущий час'2);
  42.         }
  43.         if (true === $this->features->dynamic_prices()) {
  44.             $placementPrice $this->paidPlacementPriceRepository->getDynamicProfileTopPlacementPrice($profile);
  45.             if (null === $placementPrice) {
  46.                 throw new \LogicException('Dynamic profile top placement price not found');
  47.             }
  48.         } else {
  49.             $placementPrice $this->paidPlacementPriceRepository->getProfileTopPlacementPrice($profile);
  50.             if (null === $placementPrice) {
  51.                 throw new \LogicException('Profile top placement price not found');
  52.             }
  53.         }
  54.         $charges $this->profileChargesCalculator->calculateTopPlacementCharges($profile$placedAt$placedUntil);
  55.         $placement = new TopPlacement($city$profile$placementPrice$placedAt$placedUntil);
  56.         $placementCharge = new PlacementCharge($profile$chargesCarbonImmutable::now(), $placementPrice$placedAt$placedUntil);
  57.         $this->entityManager->transactional(function (EntityManagerInterface $em) use ($city$placement$placementCharge$profile$placedAt$placedUntil): void {
  58.             $overlaps $this->profileTopPlacementRepository->getPlacementsByPeriod($city$placedAt$placedUntil);
  59.             if(count($overlaps)) {
  60.                 throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  61.             }
  62.             $this->accountFinances->processCharge($placementCharge);
  63.             $em->persist($placement);
  64.             $profile->addTopPlacement($placement);
  65.             $this->eventDispatcher->dispatch(new ProfileWasPlacedOnTop($profile$placement), ProfileWasPlacedOnTop::NAME);
  66.         });
  67.     }
  68.     /**
  69.      * @deprecated Теперь для показа топ-размещения учитываются фильтры текущего листинга
  70.      * @see topPlacementSatisfiedBy
  71.      */
  72.     public function currentTopPlacement(bool $increaseShows): ?Profile
  73.     {
  74.         $city $this->cityResolver->resolveCurrentCity();
  75.         $currentTime CarbonImmutable::now();
  76.         $profile $this->profileTopPlacementRepository->getCurrentlyPlaced($city$currentTime);
  77.         if($profile) {
  78.             $this->profileAdBoard->deleteProfileHiding($profile);
  79.             if($increaseShows) {
  80.                 $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'top'), ProfilesShownEvent::NAME);
  81.             }
  82.         }
  83.         return $profile;
  84.     }
  85.     public function topPlacementSatisfiedBy(City $city, ?Filter $spec null): ?Profile
  86.     {
  87.         $currentTime CarbonImmutable::now();
  88.         return $this->profileTopPlacementRepository->getCurrentlyPlacedAndSatisfiedBy($city$currentTime$spec);
  89.     }
  90. }