ArticleRepository::getArticlesSorted()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 55
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 55
rs 8.7752
cc 6
eloc 46
nc 6
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function getArticlesWithCountComment($page = 1, $max = 10)
16
    {
17
        $first = $max * ($page - 1);
18
        $query = $this->createQueryBuilder('a')
19
            ->select('a, c, t, u, count(cm.id) as countComments')
20
            ->leftJoin('a.categories', 'c')
21
            ->leftJoin('a.tags', 't')
22
            ->leftJoin('a.comments', 'cm')
23
            ->join('a.user', 'u')
24
            ->groupBy('a, c, t, u')
25
            ->orderBy('a.createdAt', 'DESC')
26
            ->setFirstResult($first)
27
            ->setMaxResults($max)
28
            ->getQuery();
29
30
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
31
    }
32
33
    public function getArticleWithDep($slug)
34
    {
35
        return $this->createQueryBuilder('a')
36
            ->select('a, c, t, u')
37
            ->leftJoin('a.categories', 'c')
38
            ->leftJoin('a.tags', 't')
39
            ->join('a.user', 'u')
40
            ->where('a.slug = ?1')
41
            ->setParameter(1, $slug)
42
            ->getQuery()
43
            ->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
    public function getArticlesSorted($sortBy, $param, $page = 1, $max = 10)
62
    {
63
        $first = $max * ($page - 1);
64
        $query = $this->createQueryBuilder('a')
65
            ->select('a, c, t, u, count(cm.id) as countComments')
66
            ->leftJoin('a.categories', 'c')
67
            ->leftJoin('a.tags', 't')
68
            ->leftJoin('a.comments', 'cm')
69
            ->join('a.user', 'u')
70
            ->groupBy('a, c, t, u')
71
            ->orderBy('a.createdAt', 'DESC');
72
73
        switch ($sortBy) {
74
            case 'category':
75
                $query
76
                    ->where('c.slug = ?1')
77
                    ->setParameter(1, $param);
78
                break;
79
            case 'tag':
80
                $query
81
                    ->where('t.slug = ?1')
82
                    ->setParameter(1, $param);
83
                break;
84
            case 'author':
85
                $query
86
                    ->where('u.slug = ?1')
87
                    ->setParameter(1, $param);
88
                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
            case 'search':
97
                $query
98
                    ->where(
99
                        $query->expr()->like(
100
                            $query->expr()->lower('a.title'),
101
                            $query->expr()->lower('?1')
102
                        )
103
                    )
104
                    ->setParameter(1, '%'.$param.'%');
105
                break;
106
        }
107
108
        $query
109
            ->setFirstResult($first)
110
            ->setMaxResults($max)
111
            ->getQuery();
112
113
        return new PaginatorWithPages($query, $fetchJoinCollection = true);
114
115
    }
116
117 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...
118
    {
119
        return $this->createQueryBuilder('a')
120
            ->select('a, u, avg(c.rating) as rating')
121
            ->leftJoin('a.comments', 'c')
122
            ->join('a.user', 'u')
123
            ->groupBy('a, u')
124
            ->orderBy('rating', 'DESC')
125
            ->setFirstResult(0)
126
            ->setMaxResults($max)
127
            ->getQuery()
128
            ->getResult();
129
    }
130
131
    public function getRecentArticles($max = 5)
132
    {
133
        return $this->createQueryBuilder('a')
134
            ->select('a, u')
135
            ->join('a.user', 'u')
136
            ->orderBy('a.createdAt', 'DESC')
137
            ->setFirstResult(0)
138
            ->setMaxResults($max)
139
            ->getQuery()
140
            ->getResult();
141
    }
142
143
    public function getCountArticles()
144
    {
145
        return $this->createQueryBuilder('a')
146
            ->select('count(a.id) as countArticles')
147
            ->getQuery()
148
            ->getSingleResult();
149
    }
150
}
151