src/Repository/ProfileTopPlacementRepository.php line 23

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 Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Happyr\DoctrineSpecification\Filter\Filter;
  14. use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepositoryTrait;
  15. class ProfileTopPlacementRepository extends ServiceEntityRepository
  16. {
  17.     use EntitySpecificationRepositoryTrait;
  18.     private ProfileRepository $profileRepository;
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registryTopPlacement::class);
  22.         $this->profileRepository $registry->getRepository(Profile::class);
  23.     }
  24.     public function getCurrentlyPlaced(City $city\DateTimeInterface $currentTime): ?Profile
  25.     {
  26.         $qb $this->createQueryBuilder('top_profile_placement')
  27.             ->select('top_profile_placement, profile'//, photo
  28.             ->join('top_profile_placement.profile''profile')
  29. //            ->join('profile.photos', 'photo')
  30.             ->andWhere('profile.city = :city')
  31.             ->andWhere('top_profile_placement.placedAt <= :currentTime AND :currentTime < top_profile_placement.expiresAt')
  32.             ->setParameter('city'$city->getId())
  33.             ->setParameter('currentTime'$currentTime)
  34.             ->setMaxResults(1);
  35.         $placement $qb->getQuery()->getOneOrNullResult();
  36.         return $placement $placement->getProfile() : null;
  37.     }
  38.     public function getCurrentlyPlacedAndSatisfiedBy(City $city\DateTimeInterface $currentTime, ?Filter $spec null): ?Profile
  39.     {
  40.         $qb $this->profileRepository->createQueryBuilder($dqlAlias 'p')
  41.             ->select('top_profile_placement, p'//, photo
  42.             ->join('p.topPlacements''top_profile_placement')
  43. //            ->join('p.photos', 'photo')
  44.             ->andWhere('p.city = :city')
  45.             ->andWhere(':currentTime BETWEEN top_profile_placement.placedAt AND top_profile_placement.expiresAt')
  46.             ->setParameter('city'$city->getId())
  47.             ->setParameter('currentTime'$currentTime)
  48.             ->setMaxResults(1);
  49.         if (null !== $spec) {
  50.             $this->applySpecification($qb$spec$dqlAlias);
  51.         }
  52.         $profiles $qb->getQuery()->getResult();
  53.         return $profiles[0] ?? null;
  54.     }
  55.     public function getPlacementsByPeriod(City $city\DateTimeInterface $dateStart\DateTimeInterface $dateEnd): array
  56.     {
  57.         $qb $this->createQueryBuilder('top')
  58.             ->andWhere('top.city = :city')
  59.             ->setParameter('city'$city->getId())
  60.         ;
  61. /*
  62.         $orX = $qb->expr()->orX(
  63.             'top.placedAt <= :dateStart AND :dateStart < top.expiresAt',
  64.             'top.placedAt < :dateEnd AND :dateEnd <= top.expiresAt',
  65.             ':dateStart <= top.placedAt AND top.placedAt < :dateEnd',
  66.             ':dateStart < top.expiresAt AND top.expiresAt <= :dateEnd'
  67.         );
  68. */
  69.         $orX $qb->expr()->orX(
  70.             ':dateStart <= top.placedAt AND top.placedAt < :dateEnd',
  71.             ':dateStart < top.expiresAt AND top.expiresAt <= :dateEnd',
  72.             'top.placedAt <= :dateStart AND :dateEnd <= top.expiresAt',
  73.         );
  74.         $qb->andWhere($orX)
  75.             ->setParameter('dateStart'$dateStart)
  76.             ->setParameter('dateEnd'$dateEnd)
  77.         ;
  78.         return $qb->getQuery()->getResult();
  79.     }
  80. }