Passed
Pull Request — master (#106)
by
unknown
07:55
created

TrickRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Trick;
6
use App\Pagination\PaginateRepositoryTrait;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
9
use InvalidArgumentException;
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
use Doctrine\ORM\Tools\Pagination\Paginator;
12
13
/**
14
 * @method Trick|null find($id, $lockMode = null, $lockVersion = null)
15
 * @method Trick|null findOneBy(array $criteria, array $orderBy = null)
16
 * @method Trick[]    findAll()
17
 * @method Trick[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
18
 */
19
class TrickRepository extends ServiceEntityRepository
20
{
21
    use PaginateRepositoryTrait;
22
23
    public function __construct(RegistryInterface $registry)
24
    {
25
        parent::__construct($registry, Trick::class);
26
    }
27
28
    /**
29
     * @param int $currentPage
30
     * @param int $categoryId
31
     * @return Paginator
32
     */
33
    public function findLatestEdited(int $currentPage = 1, int $categoryId = 0)
34
    {
35
        if($currentPage <1){
36
            throw new InvalidArgumentException("Current page can not be lower than one");
37
        }
38
39
        $query = $this->createQueryBuilder('t');
40
41
        if($categoryId>0){
42
            $query->where('t.category = :categoryId')
43
                ->setParameter('categoryId', $categoryId);
44
        }
45
46
        $query->orderBy('t.editedAt', 'DESC')
47
            ->getQuery();
48
        $paginator = $this->paginate($query,Trick::NUMBER_OF_DISPLAYED_TRICKS, $currentPage);
49
50
        return $paginator;
51
52
    }
53
54
    /**
55
     * @param int $currentPage
56
     * @param int $tagId
57
     * @return Paginator
58
     */
59
    public function findLatestEditedByTag(int $currentPage = 1, int $tagId = 0)
60
    {
61
        if($currentPage <1){
62
            throw new InvalidArgumentException("Current page can not be lower than one");
63
        }
64
65
        $query = $this->createQueryBuilder('t');
66
67
        if($tagId>0){
68
            $query->leftJoin('t.tags', 'tag')
69
                ->where('tag = :tagId')
70
                ->setParameter('tagId', $tagId);
71
        }
72
73
        $query->orderBy('t.editedAt', 'DESC')
74
            ->getQuery();
75
        $paginator = $this->paginate($query,Trick::NUMBER_OF_DISPLAYED_TRICKS, $currentPage);
76
77
        return $paginator;
78
79
    }
80
81
    public function findBySearchQuery(array $searchTerms)
82
    {
83
84
        $queryBuilder = $this->createQueryBuilder('p');
85
86
        foreach ($searchTerms as $key => $term) {
87
            $queryBuilder
88
                ->orWhere('p.name LIKE :term_' . $key)
89
                ->setParameter('term_' . $key, '%' . $term . '%');
90
91
        }
92
93
        return $queryBuilder
94
            ->orderBy('p.createdAt', 'DESC')
95
            ->getQuery()
96
            ->getResult();
97
    }
98
}
99