src/Repository/Comment/CommentRepository.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Slivki\Repository\Comment;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\Query\Expr\Join;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Slivki\Entity\City;
  8. use Slivki\Entity\Comment;
  9. use Slivki\Entity\Offer;
  10. use Slivki\Exception\Comment\CommentNotFountException;
  11. final class CommentRepository extends ServiceEntityRepository implements CommentRepositoryInterface
  12. {
  13.     public function __construct(ManagerRegistry $registry)
  14.     {
  15.         parent::__construct($registryComment::class);
  16.     }
  17.     public function getById(int $id): Comment
  18.     {
  19.         $comment $this->findById($id);
  20.         if (!$comment instanceof Comment) {
  21.             throw new CommentNotFountException();
  22.         }
  23.         return $comment;
  24.     }
  25.     public function findById(int $id): ?Comment
  26.     {
  27.         $qb $this->createQueryBuilder('c');
  28.         $expr $qb->expr();
  29.         return $qb
  30.             ->andWhere($expr->eq('c.ID'':id'))
  31.             ->setParameters([
  32.                 'id' => $id,
  33.             ])
  34.             ->getQuery()
  35.             ->getOneOrNullResult();
  36.     }
  37.     public function getLastOfferComments(int $limit): array
  38.     {
  39.         $qb $this->createQueryBuilder('comment');
  40.         $expr $qb->expr();
  41.         return $qb
  42.             ->innerJoin(Offer::class, 'offer'Join::WITH$expr->eq('offer.ID''comment.entityID'))
  43.             ->andWhere($expr->eq('comment.hidden'':hidden'))
  44.             ->andWhere($expr->eq('comment.confirmedPhone'':confirmedPhone'))
  45.             ->andWhere($expr->neq('offer.defaultCity'':tashkentCityId'))
  46.             ->setParameters([
  47.                 'hidden' => false,
  48.                 'confirmedPhone' => true,
  49.                 'tashkentCityId' => City::TASHKENT_CITY_ID,
  50.             ])
  51.             ->addOrderBy($expr->desc('comment.createdOn'))
  52.             ->setMaxResults($limit)
  53.             ->getQuery()
  54.             ->getResult();
  55.     }
  56.     public function getOfferCommentByIdAndOfferDirector(int $commentIdint $offerDirectorId): Comment
  57.     {
  58.         $qb $this->createQueryBuilder('comment');
  59.         $expr $qb->expr();
  60.         $comment $qb
  61.             ->innerJoin(Offer::class, 'offer'Join::WITH$expr->eq('offer.ID''comment.entityID'))
  62.             ->innerJoin('offer.directors''director')
  63.             ->andWhere($expr->eq('comment.ID'':commentId'))
  64.             ->andWhere($expr->eq('director'':directorId'))
  65.             ->setParameters([
  66.                 'commentId' => $commentId,
  67.                 'directorId' => $offerDirectorId,
  68.             ])
  69.             ->getQuery()
  70.             ->getOneOrNullResult();
  71.         if (!$comment instanceof Comment) {
  72.             throw new CommentNotFountException();
  73.         }
  74.         return $comment;
  75.     }
  76.     public function save(Comment $comment): void
  77.     {
  78.         $entityManager $this->getEntityManager();
  79.         $entityManager->persist($comment);
  80.         $entityManager->flush($comment);
  81.     }
  82. }