src/Service/ProfileTopBoard.php line 142

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.         $this->doPlaceOnTopMany($profile, [[$placedAt$placedUntil]]);
  38.     }
  39.     public function doPlaceOnTopMany(Profile $profile, array $periods): void
  40.     {
  41.         if (empty($periods)) {
  42.             throw new \LogicException('Не выбрано время размещения.'2);
  43.         }
  44.         $city $profile->getCity();
  45.         if (true === $this->features->dynamic_prices()) {
  46.             $placementPrice $this->paidPlacementPriceRepository->getDynamicProfileTopPlacementPrice($profile);
  47.             if (null === $placementPrice) {
  48.                 throw new \LogicException('Dynamic profile top placement price not found');
  49.             }
  50.         } else {
  51.             $placementPrice $this->paidPlacementPriceRepository->getProfileTopPlacementPrice($profile);
  52.             if (null === $placementPrice) {
  53.                 throw new \LogicException('Profile top placement price not found');
  54.             }
  55.         }
  56.         $placementCharges = [];
  57.         $placements = [];
  58.         $requestedSlots = [];
  59.         $chargeDate CarbonImmutable::now();
  60.         foreach ($periods as [$placedAt$placedUntil]) {
  61.             if ($placedAt >= $placedUntil) {
  62.                 throw new \LogicException('Некорректный период размещения.'2);
  63.             }
  64.             $timezone $placedAt->getTimezone();
  65.             $now = new \DateTime('now'$timezone);
  66.             if($placedAt->format("Y-m-d H") == $now->format("Y-m-d H")) {
  67.                 throw new \LogicException('Нельзя разместить на текущий час'2);
  68.             }
  69.             $charges $this->profileChargesCalculator->calculateTopPlacementCharges($profile$placedAt$placedUntil);
  70.             $placementCharges[] = new PlacementCharge($profile$charges$chargeDate$placementPrice$placedAt$placedUntil);
  71.             $intervalStart = clone $placedAt;
  72.             while ($intervalStart $placedUntil) {
  73.                 $intervalEnd $intervalStart->modify('+1 hour');
  74.                 $slotKey $intervalStart->format('U');
  75.                 if (isset($requestedSlots[$slotKey])) {
  76.                     throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  77.                 }
  78.                 $requestedSlots[$slotKey] = true;
  79.                 $placements[] = new TopPlacement($city$profile$placementPrice$intervalStart$intervalEnd);
  80.                 $intervalStart $intervalEnd;
  81.             }
  82.         }
  83.         $this->entityManager->transactional(function (EntityManagerInterface $em) use ($city$periods$placements$placementCharges$profile): void {
  84.             foreach ($periods as [$placedAt$placedUntil]) {
  85.                 $overlaps $this->profileTopPlacementRepository->getPlacementsByPeriod($city$placedAt$placedUntil);
  86.                 if(count($overlaps)) {
  87.                     throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  88.                 }
  89.             }
  90.             foreach ($placementCharges as $placementCharge) {
  91.                 $this->accountFinances->processCharge($placementCharge);
  92.             }
  93.             foreach ($placements as $placement) {
  94.                 $em->persist($placement);
  95.                 $profile->addTopPlacement($placement);
  96.                 $this->eventDispatcher->dispatch(new ProfileWasPlacedOnTop($profile$placement), ProfileWasPlacedOnTop::NAME);
  97.             }
  98.         });
  99.     }
  100.     /**
  101.      * @deprecated Теперь для показа топ-размещения учитываются фильтры текущего листинга
  102.      * @see topPlacementSatisfiedBy
  103.      */
  104.     public function currentTopPlacement(bool $increaseShows): ?Profile
  105.     {
  106.         $city $this->cityResolver->resolveCurrentCity();
  107.         $currentTime CarbonImmutable::now();
  108.         $profile $this->profileTopPlacementRepository->getCurrentlyPlaced($city$currentTime);
  109.         if($profile) {
  110.             $this->profileAdBoard->deleteProfileHiding($profile);
  111.             if($increaseShows) {
  112.                 $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'top'), ProfilesShownEvent::NAME);
  113.             }
  114.         }
  115.         return $profile;
  116.     }
  117.     public function topPlacementSatisfiedBy(City $city, ?Filter $spec null): ?Profile
  118.     {
  119.         $currentTime CarbonImmutable::now();
  120.         return $this->profileTopPlacementRepository->getCurrentlyPlacedAndSatisfiedBy($city$currentTime$spec);
  121.     }
  122. }