<?php
/**
* Created by simpson <simpsonwork@gmail.com>
* Date: 2019-04-25
* Time: 13:39
*/
namespace App\Repository;
use App\Entity\Location\City;
use App\Entity\Profile\Profile;
use App\Entity\Sales\Profile\TopPlacement;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
class ProfileTopPlacementRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TopPlacement::class);
}
public function getCurrentlyPlaced(City $city, \DateTimeInterface $currentTime): ?Profile
{
$qb = $this->createQueryBuilder('top_profile_placement')
->select('top_profile_placement, profile') //, photo
->join('top_profile_placement.profile', 'profile')
// ->join('profile.photos', 'photo')
->andWhere('profile.city = :city')
->andWhere('top_profile_placement.placedAt <= :currentTime AND :currentTime < top_profile_placement.expiresAt')
->setParameter('city', $city->getId())
->setParameter('currentTime', $currentTime)
->setMaxResults(1);
;
$placement = $qb->getQuery()->getOneOrNullResult();
return $placement ? $placement->getProfile() : null;
}
public function getPlacementsByPeriod(City $city, \DateTimeInterface $dateStart, \DateTimeInterface $dateEnd): array
{
$qb = $this->createQueryBuilder('top')
->andWhere('top.city = :city')
->setParameter('city', $city->getId())
;
$orX = $qb->expr()->orX(
'top.placedAt <= :dateStart AND :dateStart < top.expiresAt',
'top.placedAt < :dateEnd AND :dateEnd <= top.expiresAt',
':dateStart <= top.placedAt AND top.placedAt < :dateEnd',
':dateStart < top.expiresAt AND top.expiresAt <= :dateEnd'
);
$qb->andWhere($orX)
->setParameter('dateStart', $dateStart)
->setParameter('dateEnd', $dateEnd)
;
return $qb->getQuery()->getResult();
}
}