Completed
Push — develop ( 6d3372...82247e )
by Victor
02:40
created

ArticleRepository::getArticlesSorted()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 45
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5.1647

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 45
ccs 26
cts 32
cp 0.8125
rs 8.439
cc 5
eloc 38
nc 5
nop 4
crap 5.1647
1
<?php
2
3
namespace AppBundle\Repository;
4
use AppBundle\Model\PaginatorWithPages;
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * ArticleRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class ArticleRepository extends EntityRepository
14
{
15 1
    public function getArticlesWithCountComment($page = 1, $max = 10)
16
    {
17 1
        $first = $max * ($page - 1);
18 1
        $query = $this->createQueryBuilder('a')
19 1
            ->select('a, c, t, u, count(cm.id) as countComments')
20 1
            ->leftJoin('a.categories', 'c')
21 1
            ->leftJoin('a.tags', 't')
22 1
            ->leftJoin('a.comments', 'cm')
23 1
            ->join('a.user', 'u')
24 1
            ->groupBy('a, c, t, u')
25 1
            ->orderBy('a.createdAt', 'DESC')
26 1
            ->setFirstResult($first)
27 1
            ->setMaxResults($max)
28 1
            ->getQuery();
29
30 1
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
31
    }
32
33 1
    public function getArticleWithDep($slug)
34
    {
35 1
        return $this->createQueryBuilder('a')
36 1
            ->select('a, c, t, u')
37 1
            ->leftJoin('a.categories', 'c')
38 1
            ->leftJoin('a.tags', 't')
39 1
            ->join('a.user', 'u')
40 1
            ->where('a.slug = ?1')
41 1
            ->setParameter(1, $slug)
42 1
            ->getQuery()
43 1
            ->getSingleResult();
44
    }
45
46 View Code Duplication
    public function getArticleWithCountComment($slug)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        return $this->createQueryBuilder('a')
49
            ->select('a, c, t, u, count(cm.id) as countComments')
50
            ->leftJoin('a.categories', 'c')
51
            ->leftJoin('a.tags', 't')
52
            ->leftJoin('a.comments', 'cm')
53
            ->join('a.user', 'u')
54
            ->where('a.slug = ?1')
55
            ->groupBy('a, c, t, u')
56
            ->setParameter(1, $slug)
57
            ->getQuery()
58
            ->getSingleResult();
59
    }
60
61 3
    public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10)
62
    {
63 3
        $first = $max * ($page - 1);
64 3
        $query = $this->createQueryBuilder('a')
65 3
            ->select('a, c, t, u, count(cm.id) as countComments')
66 3
            ->leftJoin('a.categories', 'c')
67 3
            ->leftJoin('a.tags', 't')
68 3
            ->leftJoin('a.comments', 'cm')
69 3
            ->join('a.user', 'u')
70 3
            ->groupBy('a, c, t, u')
71 3
            ->orderBy('a.createdAt', 'DESC');
72
73
        switch ($sortBy) {
74 3
            case 'category':
75
                $query
76 1
                    ->where('c.slug = ?1')
77 1
                    ->setParameter(1, $param);
78 1
                break;
79 2
            case 'tag':
80
                $query
81 1
                    ->where('t.slug = ?1')
82 1
                    ->setParameter(1, $param);
83 1
                break;
84 1
            case 'author':
85
                $query
86 1
                    ->where('u.slug = ?1')
87 1
                    ->setParameter(1, $param);
88 1
                break;
89
            case 'date':
90
                $query
91
                    ->where('a.createdAt >= ?1')
92
                    ->andWhere('a.createdAt <= ?2')
93
                    ->setParameter(1, $param." 00:00:00")
94
                    ->setParameter(2, $param." 23:59:59");
95
                break;
96
        }
97
98
        $query
99 3
            ->setFirstResult($first)
100 3
            ->setMaxResults($max)
101 3
            ->getQuery();
102
103 3
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
104
105
    }
106
107 5 View Code Duplication
    public function getPopularArticles($max = 5)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 5
        return $this->createQueryBuilder('a')
110 5
            ->select('a, u, avg(c.rating) as rating')
111 5
            ->leftJoin('a.comments', 'c')
112 5
            ->join('a.user', 'u')
113 5
            ->groupBy('a, u')
114 5
            ->orderBy('rating', 'DESC')
115 5
            ->setFirstResult(0)
116 5
            ->setMaxResults($max)
117 5
            ->getQuery()
118 5
            ->getResult();
119
    }
120
121 5
    public function getRecentArticles($max = 5)
122
    {
123 5
        return $this->createQueryBuilder('a')
124 5
            ->select('a, u')
125 5
            ->join('a.user', 'u')
126 5
            ->orderBy('a.createdAt', 'DESC')
127 5
            ->setFirstResult(0)
128 5
            ->setMaxResults($max)
129 5
            ->getQuery()
130 5
            ->getResult();
131
    }
132
133
    public function getCountArticles()
134
    {
135
        return $this->createQueryBuilder('a')
136
            ->select('count(a.id) as countArticles')
137
            ->getQuery()
138
            ->getSingleResult();
139
    }
140
}
141