src/Service/ProfileTopBoard.php line 77

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 AccountFinances $accountFinances,
  26.         private ProfileTopPlacementRepository $profileTopPlacementRepository,
  27.         private EventDispatcherInterface $eventDispatcher,
  28.         private CurrentCityResolver $cityResolver,
  29.         private ProfileAdBoard $profileAdBoard
  30.     )
  31.     {
  32.         $this->entityManager $managerRegistry->getManagerForClass(TopPlacement::class);
  33.     }
  34.     public function doPlaceOnTop(Profile $profile\DateTimeImmutable $placedAt\DateTimeImmutable $placedUntil): void
  35.     {
  36.         $city $profile->getCity();
  37.         $timezone $placedAt->getTimezone();
  38.         $now = new \DateTime('now'$timezone);
  39.         if($placedAt->format("Y-m-d H") == $now->format("Y-m-d H")) {
  40.             throw new \LogicException('Нельзя разместить на текущий час'2);
  41.         }
  42.         $placementPrice $this->paidPlacementPriceRepository->getProfileTopPlacementPrice($profile);
  43.         $charges $this->profileChargesCalculator->calculateTopPlacementCharges($profile$placedAt$placedUntil);
  44.         $placement = new TopPlacement($city$profile$placementPrice$placedAt$placedUntil);
  45.         $placementCharge = new PlacementCharge($profile$chargesCarbonImmutable::now(), $placementPrice$placedAt$placedUntil);
  46.         $this->entityManager->transactional(function (EntityManagerInterface $em) use ($city$placement$placementCharge$profile$placedAt$placedUntil): void {
  47.             $overlaps $this->profileTopPlacementRepository->getPlacementsByPeriod($city$placedAt$placedUntil);
  48.             if(count($overlaps)) {
  49.                 throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  50.             }
  51.             $this->accountFinances->processCharge($placementCharge);
  52.             $em->persist($placement);
  53.             $profile->addTopPlacement($placement);
  54.             $this->eventDispatcher->dispatch(new ProfileWasPlacedOnTop($profile$placement), ProfileWasPlacedOnTop::NAME);
  55.         });
  56.     }
  57.     public function currentTopPlacement(bool $increaseShows): ?Profile
  58.     {
  59.         $city $this->cityResolver->resolveCurrentCity();
  60.         $currentTime CarbonImmutable::now();
  61.         $profile $this->profileTopPlacementRepository->getCurrentlyPlaced($city$currentTime);
  62.         if($profile) {
  63.             $this->profileAdBoard->deleteProfileHiding($profile);
  64.             if($increaseShows) {
  65.                 $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'top'), ProfilesShownEvent::NAME);
  66.             }
  67.         }
  68.         return $profile;
  69.     }
  70.     public function topPlacementSatisfiedBy(City $city, ?Filter $spec null): ?Profile
  71.     {
  72.         $currentTime CarbonImmutable::now();
  73.         return $this->profileTopPlacementRepository->getCurrentlyPlacedAndSatisfiedBy($city$currentTime$spec);
  74.     }
  75. }