<?php
namespace WebBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\ORM\UnexpectedResultException;
use Doctrine\Persistence\ManagerRegistry;
use FlexApp\Constant\TimeConstant;
use WebBundle\Entity\ProductReviewsCache;
use WebBundle\Traits\RepoTrait;
/**
* @method ProductReviewsCache|null find($id, $lockMode = null, $lockVersion = null)
*/
class ProductReviewsCacheRepository extends ServiceEntityRepository
{
use RepoTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ProductReviewsCache::class);
}
/**
* @param int $id
* @return null
* @throws NonUniqueResultException
*/
public function getReview(int $id)
{
$q = $this->createQueryBuilder('r')
->select('r')
->where('r.id = :id')
->setParameter('id', $id)
->setMaxResults(1)
->getQuery()
->enableResultCache(TimeConstant::HOUR);
try {
return $q->getSingleResult();
} catch (NoResultException $e) {
return null;
}
}
/**
* @param null $param
* @return array|null
*/
public function getReviews($param = null): ?array
{
$q = $this->createQueryBuilder('r')
->select('r');
if (!empty($param['referenceId'])) {
$q->andWhere('r.referenceId = \'' . $param['referenceId'] . '\'');
}
if (!empty($param['conversationId'])) {
if (!is_array($param['conversationId'])) {
$param['conversationId'] = [$param['conversationId']];
}
$q->andWhere('r.conversationId IN (:conversationId)')
->setParameter('conversationId', $param['conversationId']);
}
if (!empty($param['productId'])) {
$q->andWhere('r.p_id = \'' . $param['productId'] . '\'');
}
if (!empty($param['sku'])) {
if (!is_array($param['sku'])) {
$param['sku'] = [$param['sku']];
}
$q->andWhere('r.sku IN (\'' . join('\',\'', $param['sku']) . '\')');
}
$r = $q->getQuery()
->enableResultCache(TimeConstant::HOUR);
return $r->getArrayResult();
}
public function getReviewsForReviewsReport($offset = null, $limit = null): iterable
{
$q = $this->createQueryBuilder('r')
->select('r')
->leftJoin('r.collection', 'c')
->leftJoin('r.order', 'o')
->addSelect('c')
->addSelect('o')
->leftJoin('o.mailing', 'm')
->addSelect('m')
->orderBy('r.id', 'DESC')
;
if (null !== $offset) {
$q->setFirstResult($offset);
}
if (null !== $limit) {
$q->setMaxResults($limit);
}
return $q->getQuery()->getResult();
}
public function getReviewsTotalCount()
{
try {
$result = $this->createQueryBuilder('r')
->select('count(r.id)')
->getQuery()
->getSingleScalarResult();
} catch (UnexpectedResultException $e) {
$result = 0;
}
return $result;
}
public function save(ProductReviewsCache $productReview, bool $flush = true): void
{
$this->getEntityManager()->persist($productReview);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function getReviewsForFeeds(): array
{
try {
$query = $this->createQueryBuilder('r')
// ->where('r.collection IS NOT NULL')
->where('r.collection <> 0')
->orderBy('r.collection', 'ASC')
->getQuery()
;
$sql = $query->getSQL();
$result = $query->getResult();
} catch (UnexpectedResultException $e) {
$result = [];
}
return $result;
}
}