Completed
Push — master ( 4ea9b4...70e6b0 )
by Serhii
04:57
created

PostRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Post;
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
8
class PostRepository extends AbstractRepository
9
{
10 6
    public function __construct(ManagerRegistry $registry)
11
    {
12 6
        parent::__construct($registry, Post::class);
13 6
    }
14
15
    /**
16
     * @param int $limit
17
     * @param int $page
18
     * @param string $tagSlug
19
     * @return Post[]
20
     */
21 3
    public function findAllOrByTag($limit, $page, $tagSlug = null)
22
    {
23 3
        $taggedPosts = $this->getTaggedPostsQuery($tagSlug)
24 3
            ->setMaxResults($limit)
25 3
            ->setFirstResult($page)
26 3
            ->orderBy('u.createdAt', 'DESC')
27 3
            ->getQuery()
28 3
            ->enableResultCache(60*60*24)
29 3
            ->execute()
30
        ;
31
32 3
        $pinnedPosts = $this->getTaggedPostsQuery($tagSlug, true)->getQuery()->execute();
33
34 3
        return array_merge($pinnedPosts, $taggedPosts);
35
    }
36
37
    /**
38
     * @param string $tagSlug
39
     * @return int
40
     */
41 3 View Code Duplication
    public function getCount($tagSlug = null)
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...
42
    {
43 3
        $qb = $this->getTaggedPostsQuery($tagSlug);
44 3
        $qb->select($qb->expr()->count('u'));
45
46 3
        $query = $qb->getQuery()->enableResultCache(60*60*24);
47
48 3
        return $query->getSingleScalarResult();
49
    }
50
51
    /**
52
     * @param string $tagSlug
53
     * @param bool $pinned
54
     * @return \Doctrine\ORM\QueryBuilder
55
     */
56 3
    protected function getTaggedPostsQuery($tagSlug = null, $pinned = false)
57
    {
58 3
        $qb = $this->createQueryBuilder('u')
59 3
            ->andWhere('u.pinned = :pinned')
60 3
            ->setParameter('pinned', $pinned)
61
        ;
62
63 3
        if ($tagSlug) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tagSlug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
64
            $qb->join('u.tags', 't')->andWhere('t.slug = :slug')->setParameter('slug', $tagSlug);
65
        }
66
67 3
        return $qb;
68
    }
69
}
70