src/Service/ProfileTopBoard.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Profile\Profile;
  4. use App\Entity\Sales\Profile\PlacementCharge;
  5. use App\Entity\Sales\Profile\TopPlacement;
  6. use App\Event\Profile\ProfilesShownEvent;
  7. use App\Event\Profile\ProfileWasPlacedOnTop;
  8. use App\Repository\PaidPlacementPriceRepository;
  9. use App\Repository\ProfileTopPlacementRepository;
  10. use Carbon\CarbonImmutable;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. class ProfileTopBoard
  16. {
  17.     private ?ObjectManager $entityManager;
  18.     public function __construct(
  19.         ManagerRegistry $managerRegistry,
  20.         private ProfileChargesCalculator $profileChargesCalculator,
  21.         private PaidPlacementPriceRepository  $paidPlacementPriceRepository,
  22.         private AccountFinances $accountFinances,
  23.         private ProfileTopPlacementRepository $profileTopPlacementRepository,
  24.         private EventDispatcherInterface $eventDispatcher,
  25.         private CurrentCityResolver $cityResolver,
  26.         private ProfileAdBoard $profileAdBoard
  27.     )
  28.     {
  29.         $this->entityManager $managerRegistry->getManagerForClass(TopPlacement::class);
  30.     }
  31.     public function doPlaceOnTop(Profile $profile\DateTimeImmutable $placedAt\DateTimeImmutable $placedUntil): void
  32.     {
  33.         $city $profile->getCity();
  34.         $overlaps $this->profileTopPlacementRepository->getPlacementsByPeriod($city$placedAt$placedUntil);
  35.         if(count($overlaps))
  36.             throw new \LogicException('Некоторые из выбранных промежутков уже заняты.'1);
  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.         $placementPrice $this->paidPlacementPriceRepository->getProfileTopPlacementPrice($profile);
  42.         $charges $this->profileChargesCalculator->calculateTopPlacementCharges($profile$placedAt$placedUntil);
  43.         $placement = new TopPlacement($city$profile$placementPrice$placedAt$placedUntil);
  44.         $placementCharge = new PlacementCharge($profile$chargesCarbonImmutable::now(), $placementPrice$placedAt$placedUntil);
  45.         $this->entityManager->transactional(function (EntityManagerInterface $em) use ($city$placement$placementCharge$profile): void {
  46.             $this->accountFinances->processCharge($placementCharge);
  47.             $em->persist($placement);
  48.             $profile->addTopPlacement($placement);
  49.             $this->eventDispatcher->dispatch(new ProfileWasPlacedOnTop($profile$placement), ProfileWasPlacedOnTop::NAME);
  50.         });
  51.     }
  52.     public function currentTopPlacement(bool $increaseShows): ?Profile
  53.     {
  54.         $city $this->cityResolver->resolveCurrentCity();
  55.         $currentTime CarbonImmutable::now();
  56.         $profile $this->profileTopPlacementRepository->getCurrentlyPlaced($city$currentTime);
  57.         if($profile) {
  58.             $this->profileAdBoard->deleteProfileHiding($profile);
  59.             if($increaseShows) {
  60.                 $this->eventDispatcher->dispatch(new ProfilesShownEvent([$profile->getId()], 'top'), ProfilesShownEvent::NAME);
  61.             }
  62.         }
  63.         return $profile;
  64.     }
  65. }