Passed
Push — develop ( 605aab...b17d99 )
by Stone
06:54 queued 11s
created

CommentRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findLatestEdited() 0 15 2
A __construct() 0 3 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Comment;
6
use App\Pagination\PaginateRepositoryTrait;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Doctrine\ORM\Tools\Pagination\Paginator;
9
use InvalidArgumentException;
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
12
/**
13
 * @method Comment|null find($id, $lockMode = null, $lockVersion = null)
14
 * @method Comment|null findOneBy(array $criteria, array $orderBy = null)
15
 * @method Comment[]    findAll()
16
 * @method Comment[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17
 */
18
class CommentRepository extends ServiceEntityRepository
19
{
20
    use PaginateRepositoryTrait;
21
22
    public function __construct(RegistryInterface $registry)
23
    {
24
        parent::__construct($registry, Comment::class);
25
    }
26
27
    public function findLatestEdited(int $trickId, int $currentPage = 1)
28
    {
29
        if ($currentPage < 1) {
30
            throw new InvalidArgumentException("Current page can not be lower than one");
31
        }
32
33
        $query = $this->createQueryBuilder('c')
34
            ->where('c.trick = :trickId')
35
            ->setParameter('trickId', $trickId)
36
            ->orderBy('c.createdAt', 'DESC')
37
            ->getQuery();
38
39
        $paginator = $this->paginate($query,Comment::NUMBER_OF_DISPLAYED_COMMENTS, $currentPage);
40
41
        return $paginator;
42
    }
43
}
44