UserLogsRepository   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
dl 0
loc 21
rs 10
c 1
b 0
f 1
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A findAllPaginated() 0 14 3
1
<?php
2
3
namespace PiouPiou\RibsAdminBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Tools\Pagination\Paginator;
7
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8
9
class UserLogsRepository extends EntityRepository
10
{
11
    /**
12
     * @param int $page
13
     * @param int $max_per_page
14
     * @return Paginator
15
     */
16
    public function findAllPaginated(int $page, int $max_per_page): Paginator
17
    {
18
        $qb = $this->createQueryBuilder('l')->orderBy('l.created_at', 'DESC');
19
        $query = $qb->getQuery();
20
21
        $first_result = ($page - 1) * $max_per_page;
22
        $query->setFirstResult($first_result)->setMaxResults($max_per_page);
23
        $paginator = new Paginator($query);
24
25
        if (($paginator->count() <= $first_result) && $page != 1) {
26
            throw new NotFoundHttpException('La page demandée n\'existe pas.');
27
        }
28
29
        return $paginator;
30
    }
31
}
32