src/Repository/ProfileTopPlacementRepository.php line 24

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-04-25
  5.  * Time: 13:39
  6.  */
  7. namespace App\Repository;
  8. use App\Entity\Location\City;
  9. use App\Entity\Profile\Profile;
  10. use App\Entity\Sales\Profile\TopPlacement;
  11. use App\Specification\Profile\ProfileIsModerationPassed;
  12. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Happyr\DoctrineSpecification\Filter\Filter;
  15. use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryTrait;
  16. class ProfileTopPlacementRepository extends ServiceEntityRepository
  17. {
  18.     use EntitySpecificationRepositoryTrait;
  19.     private ProfileRepository $profileRepository;
  20.     public function __construct(ManagerRegistry $registry)
  21.     {
  22.         parent::__construct($registry, TopPlacement::class);
  23.         $this->profileRepository = $registry->getRepository(Profile::class);
  24.     }
  25.     public function getCurrentlyPlaced(City $city, \DateTimeInterface $currentTime): ?Profile
  26.     {
  27.         $qb = $this->createQueryBuilder('top_profile_placement')
  28.             ->select('top_profile_placement, profile') //, photo
  29.             ->join('top_profile_placement.profile', 'profile')
  30. //            ->join('profile.photos', 'photo')
  31.             ->andWhere('profile.city = :city')
  32.             ->andWhere('top_profile_placement.placedAt <= :currentTime AND :currentTime < top_profile_placement.expiresAt')
  33.             ->setParameter('city', $city->getId())
  34.             ->setParameter('currentTime', $currentTime)
  35.             ->setMaxResults(1);
  36.         $this->applySpecification($qb, new ProfileIsModerationPassed(), 'profile');
  37.         $placement = $qb->getQuery()->getOneOrNullResult();
  38.         return $placement ? $placement->getProfile() : null;
  39.     }
  40.     public function getCurrentlyPlacedAndSatisfiedBy(City $city, \DateTimeInterface $currentTime, ?Filter $spec = null): ?Profile
  41.     {
  42.         $qb = $this->profileRepository->createQueryBuilder($dqlAlias = 'p')
  43.             ->select('top_profile_placement, p') //, photo
  44.             ->join('p.topPlacements', 'top_profile_placement')
  45. //            ->join('p.photos', 'photo')
  46.             ->andWhere('p.city = :city')
  47.             ->andWhere(':currentTime BETWEEN top_profile_placement.placedAt AND top_profile_placement.expiresAt')
  48.             ->setParameter('city', $city->getId())
  49.             ->setParameter('currentTime', $currentTime)
  50.             ->setMaxResults(1);
  51.         $this->applySpecification($qb, new ProfileIsModerationPassed(), $dqlAlias);
  52.         if (null !== $spec) {
  53.             $this->applySpecification($qb, $spec, $dqlAlias);
  54.         }
  55.         $profiles = $qb->getQuery()->getResult();
  56.         return $profiles[0] ?? null;
  57.     }
  58.     public function getPlacementsByPeriod(City $city, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd): array
  59.     {
  60.         $qb = $this->createQueryBuilder('top')
  61.             ->andWhere('top.city = :city')
  62.             ->setParameter('city', $city->getId())
  63.         ;
  64. /*
  65.         $orX = $qb->expr()->orX(
  66.             'top.placedAt <= :dateStart AND :dateStart < top.expiresAt',
  67.             'top.placedAt < :dateEnd AND :dateEnd <= top.expiresAt',
  68.             ':dateStart <= top.placedAt AND top.placedAt < :dateEnd',
  69.             ':dateStart < top.expiresAt AND top.expiresAt <= :dateEnd'
  70.         );
  71. */
  72.         $orX = $qb->expr()->orX(
  73.             ':dateStart <= top.placedAt AND top.placedAt < :dateEnd',
  74.             ':dateStart < top.expiresAt AND top.expiresAt <= :dateEnd',
  75.             'top.placedAt <= :dateStart AND :dateEnd <= top.expiresAt',
  76.         );
  77.         $qb->andWhere($orX)
  78.             ->setParameter('dateStart', $dateStart)
  79.             ->setParameter('dateEnd', $dateEnd)
  80.         ;
  81.         return $qb->getQuery()->getResult();
  82.     }
  83. }