src/FlexApp/Repository/CommentRepository.php line 27

Open in your IDE?
  1. <?php
  2. namespace FlexApp\Repository;
  3. use DateInterval;
  4. use DateTime;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. use Doctrine\ORM\NoResultException;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Exception;
  10. use FlexApp\Entity\CommentEntity;
  11. use WebBundle\Traits\RepoTrait;
  12. /**
  13.  * @method CommentEntity|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method CommentEntity|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method CommentEntity[]    findAll()
  16.  * @method CommentEntity[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class CommentRepository extends ServiceEntityRepository
  19. {
  20.     use RepoTrait;
  21.     public function __construct(ManagerRegistry $registry)
  22.     {
  23.         parent::__construct($registryCommentEntity::class);
  24.     }
  25.     public function getMissingPortalCommentsArray($portalUnids): array
  26.     {
  27.         $queryBuilder $this->createQueryBuilder('c');
  28.         $missingPortalComments $queryBuilder
  29.             ->where($queryBuilder->expr()->notIn('c.unid'$portalUnids))
  30.             ->orWhere($queryBuilder->expr()->isNull('c.unid'))
  31.             ->getQuery()
  32.             ->getResult();
  33.         $resultArray = [];
  34.         /** @var CommentEntity $comment */
  35.         foreach ($missingPortalComments as $comment) {
  36.             $resultArray[] = $comment->toArray();
  37.         }
  38.         return $resultArray;
  39.     }
  40.     public function getComments(string $unidstring $localestring $sort, ?int $category null): ?array
  41.     {
  42.         $query $this->createQueryBuilder('c')
  43.             ->leftJoin('c.user''user')
  44.             ->addSelect('user')
  45.             ->andWhere('c.locale = :locale')
  46.             ->setParameter('locale'$locale);
  47.         if ($category) {
  48.             $query
  49.                 ->andWhere('c.category = :category')
  50.                 ->setParameter('category'$category)
  51.             ;
  52.         }
  53.         if ($unid) {
  54.             $query->andWhere('c.commentableUnid = :unid')
  55.                 ->setParameter('unid'$unid);
  56.         }
  57.         $r $query->orderBy('c.createdAt'strtoupper($sort))
  58.             ->getQuery()
  59.             ->useQueryCache(true);
  60.         return $r->getResult();
  61.     }
  62.     public function getCommentCountData(string $unidstring $locale, ?bool $answer true): int
  63.     {
  64.         $query $this->createQueryBuilder('c')
  65.             ->select('COUNT(c.id)')
  66.             ->leftJoin('c.user''user')
  67.             ->andWhere('c.locale = :locale')
  68.             ->andWhere('c.category = :category')
  69.             ->andWhere('c.commentableUnid = :unid')
  70.             ->setParameters([
  71.                 'category' => 1,
  72.                 'locale' => $locale,
  73.                 'unid' => $unid,
  74.                 'foreignAlias' => ''
  75.             ]);
  76.         if ($answer) {
  77.             $query->andWhere('user.foreignAlias IS NOT NULL AND user.foreignAlias <> :foreignAlias');
  78.         } else {
  79.             $query->andWhere('user.foreignAlias IS NULL OR user.foreignAlias <> :foreignAlias');
  80.         }
  81.         $r $query->getQuery();
  82.         try {
  83.             return $r->getSingleScalarResult();
  84.         } catch (NoResultException|NonUniqueResultException $e) {
  85.             return 0;
  86.         }
  87.     }
  88.     /**
  89.      * количество комментариев по $unid + $locale.
  90.      *
  91.      * @param $locale
  92.      * @param $unid
  93.      *
  94.      * @return int|mixed
  95.      *
  96.      * @throws NonUniqueResultException
  97.      * @throws NoResultException
  98.      */
  99.     public function getCountPublicationsByUnid($locale$unid)
  100.     {
  101.         $q $this->createQueryBuilder('c')
  102.             ->select('count(c.id)')
  103.             ->andWhere('c.locale = :locale')
  104.             ->setParameter('locale'$locale)
  105.             ->andWhere('c.commentableUnid = :unid')
  106.             ->setParameter('unid'$unid);
  107.         $r $q->getQuery()
  108.             ->useQueryCache(true);
  109.         return $r->getSingleScalarResult();
  110.     }
  111.     /**
  112.      * получить количество комментариев по темам
  113.      *
  114.      * @param $locale
  115.      *
  116.      * @return array|null
  117.      */
  118.     public function getCountPublications($locale)
  119.     {
  120.         $query $this->createQueryBuilder('c')
  121.             ->select('c.commentableUnid, count(c.id)')
  122.             ->andWhere('c.locale = :locale')
  123.             ->setParameter('locale'$locale)
  124.             ->groupBy('c.commentableUnid');
  125.         $r $query->getQuery();
  126.         return $r->getArrayResult();
  127.     }
  128.     /**
  129.      * @return mixed
  130.      *
  131.      * @throws Exception
  132.      */
  133.     public function getCommentsByNameAndBodyAndLocaleLastTime(string $namestring $bodystring $localeint $interval)
  134.     {
  135.         $query $this->getEntityManager()->createQuery("
  136.             SELECT c
  137.             FROM FlexApp\Entity\CommentEntity c
  138.                 WHERE c.name = :name
  139.                 AND c.body = :body
  140.                 AND c.locale = :locale
  141.                 AND c.createdAt > :date_begin
  142.         ");
  143.         $query->setParameter('name'$name);
  144.         $query->setParameter('body'$body);
  145.         $query->setParameter('locale'$locale);
  146.         $query->setParameter('date_begin'$this->generateDateBegin($interval));
  147.         return $query->getResult();
  148.     }
  149.     /**
  150.      * @return DateTime
  151.      *
  152.      * @throws Exception
  153.      */
  154.     private function generateDateBegin(int $interval)
  155.     {
  156.         $dateTime = new DateTime();
  157.         $dateTime->sub(new DateInterval(sprintf('PT%sM'$interval)));
  158.         return $dateTime;
  159.     }
  160. }