You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
<?php |
|
|
|
namespace App\Repository; |
|
|
|
use App\Entity\Visit; |
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
|
use Doctrine\DBAL\Types\Types; |
|
use Doctrine\Persistence\ManagerRegistry; |
|
|
|
/** |
|
* @extends ServiceEntityRepository<Visit> |
|
*/ |
|
class VisitRepository extends ServiceEntityRepository |
|
{ |
|
public function __construct(ManagerRegistry $registry) |
|
{ |
|
parent::__construct($registry, Visit::class); |
|
} |
|
|
|
public function save(Visit $visit, bool $flush = true): void |
|
{ |
|
$this->getEntityManager()->persist($visit); |
|
|
|
if ($flush) { |
|
$this->getEntityManager()->flush(); |
|
} |
|
} |
|
|
|
public function getVisitCountByRoute(): array |
|
{ |
|
return $this->createQueryBuilder('v') |
|
->select('v.route, COUNT(v.id) as count') |
|
->groupBy('v.route') |
|
->orderBy('count', 'DESC') |
|
->getQuery() |
|
->getResult(); |
|
} |
|
|
|
/** |
|
* Returns total number of visits since the given datetime (inclusive). |
|
*/ |
|
public function countVisitsSince(\DateTimeImmutable $since): int |
|
{ |
|
$qb = $this->createQueryBuilder('v') |
|
->select('COUNT(v.id)') |
|
->where('v.visitedAt >= :since') |
|
->setParameter('since', $since, Types::DATETIME_IMMUTABLE); |
|
|
|
return (int) $qb->getQuery()->getSingleScalarResult(); |
|
} |
|
}
|
|
|