src/Service/ProfileTopBoard.php line 115

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.         $placements = [];
  56.         $intervalStart = clone $placedAt;
  57.         while ($intervalStart $placedUntil) {
  58.             $intervalEnd $intervalStart->modify('+1 hour');
  59.             $placements[] = new TopPlacement($city$profile$placementPrice$intervalStart$intervalEnd);
  60.             $intervalStart $intervalEnd;
  61.         }
  62. //        $placement = new TopPlacement($city, $profile, $placementPrice, $placedAt, $placedUntil);
  63.         $placementCharge = new PlacementCharge($profile$chargesCarbonImmutable::now(), $placementPrice$placedAt$placedUntil);
  64.         $this->entityManager->transactional(function (EntityManagerInterface $em) use ($city$placements$placementCharge$profile$placedAt$placedUntil): void {
  65.             $overlaps $this->profileTopPlacementRepository->getPlacementsByPeriod($city$placedAt$placedUntil);
  66.             if(count($overlaps)) {
  67.                 throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  68.             }
  69.             $this->accountFinances->processCharge($placementCharge);
  70.             foreach ($placements as $placement) {
  71.                 $em->persist($placement);
  72.                 $profile->addTopPlacement($placement);
  73.                 $this->eventDispatcher->dispatch(new ProfileWasPlacedOnTop($profile$placement), ProfileWasPlacedOnTop::NAME);
  74.             }
  75.         });
  76.     }
  77.     /**
  78.      * @deprecated Теперь для показа топ-размещения учитываются фильтры текущего листинга
  79.      * @see topPlacementSatisfiedBy
  80.      */
  81.     public function currentTopPlacement(bool $increaseShows): ?Profile
  82.     {
  83.         $city $this->cityResolver->resolveCurrentCity();
  84.         $currentTime CarbonImmutable::now();
  85.         $profile $this->profileTopPlacementRepository->getCurrentlyPlaced($city$currentTime);
  86.         if($profile) {
  87.             $this->profileAdBoard->deleteProfileHiding($profile);
  88.             if($increaseShows) {
  89.                 $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'top'), ProfilesShownEvent::NAME);
  90.             }
  91.         }
  92.         return $profile;
  93.     }
  94.     public function topPlacementSatisfiedBy(City $city, ?Filter $spec null): ?Profile
  95.     {
  96.         $currentTime CarbonImmutable::now();
  97.         return $this->profileTopPlacementRepository->getCurrentlyPlacedAndSatisfiedBy($city$currentTime$spec);
  98.     }
  99. }