src/WebBundle/Repository/ProductReviewsCacheRepository.php line 84

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Repository;
  3. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  4. use Doctrine\ORM\NonUniqueResultException;
  5. use Doctrine\ORM\NoResultException;
  6. use Doctrine\ORM\UnexpectedResultException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use FlexApp\Constant\TimeConstant;
  9. use WebBundle\Entity\ProductReviewsCache;
  10. use WebBundle\Traits\RepoTrait;
  11. /**
  12.  * @method ProductReviewsCache|null find($id, $lockMode = null, $lockVersion = null)
  13.  */
  14. class ProductReviewsCacheRepository extends ServiceEntityRepository
  15. {
  16.     use RepoTrait;
  17.     public function __construct(ManagerRegistry $registry)
  18.     {
  19.         parent::__construct($registryProductReviewsCache::class);
  20.     }
  21.     /**
  22.      * @param int $id
  23.      * @return null
  24.      * @throws NonUniqueResultException
  25.      */
  26.     public function getReview(int $id)
  27.     {
  28.         $q $this->createQueryBuilder('r')
  29.             ->select('r')
  30.             ->where('r.id = :id')
  31.             ->setParameter('id'$id)
  32.             ->setMaxResults(1)
  33.             ->getQuery()
  34.             ->enableResultCache(TimeConstant::HOUR);
  35.         try {
  36.             return $q->getSingleResult();
  37.         } catch (NoResultException $e) {
  38.             return null;
  39.         }
  40.     }
  41.     /**
  42.      * @param null $param
  43.      * @return array|null
  44.      */
  45.     public function getReviews($param null): ?array
  46.     {
  47.         $q $this->createQueryBuilder('r')
  48.             ->select('r');
  49.         if (!empty($param['referenceId'])) {
  50.             $q->andWhere('r.referenceId = \'' $param['referenceId'] . '\'');
  51.         }
  52.         if (!empty($param['conversationId'])) {
  53.             if (!is_array($param['conversationId'])) {
  54.                 $param['conversationId'] = [$param['conversationId']];
  55.             }
  56.             $q->andWhere('r.conversationId IN (:conversationId)')
  57.                 ->setParameter('conversationId'$param['conversationId']);
  58.         }
  59.         if (!empty($param['productId'])) {
  60.             $q->andWhere('r.p_id = \'' $param['productId'] . '\'');
  61.         }
  62.         if (!empty($param['sku'])) {
  63.             if (!is_array($param['sku'])) {
  64.                 $param['sku'] = [$param['sku']];
  65.             }
  66.             $q->andWhere('r.sku IN (\'' join('\',\''$param['sku']) . '\')');
  67.         }
  68.         $r $q->getQuery()
  69.             ->enableResultCache(TimeConstant::HOUR);
  70.         return $r->getArrayResult();
  71.     }
  72.     public function getReviewsForReviewsReport($offset null$limit null): iterable
  73.     {
  74.         $q $this->createQueryBuilder('r')
  75.             ->select('r')
  76.             ->leftJoin('r.collection''c')
  77.             ->leftJoin('r.order''o')
  78.             ->addSelect('c')
  79.             ->addSelect('o')
  80.             ->leftJoin('o.mailing''m')
  81.             ->addSelect('m')
  82.             ->orderBy('r.id''DESC')
  83.         ;
  84.         if (null !== $offset) {
  85.             $q->setFirstResult($offset);
  86.         }
  87.         if (null !== $limit) {
  88.             $q->setMaxResults($limit);
  89.         }
  90.         return $q->getQuery()->getResult();
  91.     }
  92.     public function getReviewsTotalCount()
  93.     {
  94.         try {
  95.             $result $this->createQueryBuilder('r')
  96.                 ->select('count(r.id)')
  97.                 ->getQuery()
  98.                 ->getSingleScalarResult();
  99.         } catch (UnexpectedResultException $e) {
  100.             $result 0;
  101.         }
  102.         return $result;
  103.     }
  104.     public function save(ProductReviewsCache $productReviewbool $flush true): void
  105.     {
  106.         $this->getEntityManager()->persist($productReview);
  107.         if ($flush) {
  108.             $this->getEntityManager()->flush();
  109.         }
  110.     }
  111.     public function getReviewsForFeeds(): array
  112.     {
  113.         try {
  114.             $query $this->createQueryBuilder('r')
  115. //                ->where('r.collection IS NOT NULL')
  116.                 ->where('r.collection <> 0')
  117.                 ->orderBy('r.collection''ASC')
  118.                 ->getQuery()
  119.                 ;
  120.             $sql $query->getSQL();
  121.             $result $query->getResult();
  122.         } catch (UnexpectedResultException $e) {
  123.             $result = [];
  124.         }
  125.         return $result;
  126.     }
  127. }