Passed
Branch master (1fd71c)
by Nicolas
03:28
created

ArticleRepository::getArticlesListQueryBuilder()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 8
nop 3
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace App\Repository;
4
5
use App\Entity\Article;
6
use App\Entity\User;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Doctrine\ORM\NonUniqueResultException;
9
use Doctrine\ORM\QueryBuilder;
10
use Symfony\Bridge\Doctrine\RegistryInterface;
11
12
/**
13
 * @method Article|null find($id, $lockMode = null, $lockVersion = null)
14
 * @method Article|null findOneBy(array $criteria, array $orderBy = null)
15
 * @method Article[]    findAll()
16
 * @method Article[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
17
 */
18
class ArticleRepository extends ServiceEntityRepository
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 25
    public function __construct(RegistryInterface $registry)
24
    {
25 25
        parent::__construct($registry, Article::class);
26 25
    }
27
28
    /**
29
     * @param null|string $tag
30
     * @param null|string $authorUsername
31
     * @param null|string $favoritedByUsername
32
     *
33
     * @return QueryBuilder
34
     */
35 6
    private function getArticlesListQueryBuilder(?string $tag, ?string $authorUsername, ?string $favoritedByUsername)
36
    {
37
        $qb = $this
38 6
            ->createQueryBuilder('a')
39 6
            ->innerJoin('a.author', 'author')
40 6
            ->orderBy('a.id', 'desc');
41
42 6
        if ($tag) {
43 3
            $qb->innerJoin('a.tags', 't');
44 3
            $qb->andWhere('t.name = :tag');
45 3
            $qb->setParameter('tag', $tag);
46
        }
47
48 6
        if ($authorUsername) {
49 2
            $qb->andWhere('author.username = :author_username');
50 2
            $qb->setParameter('author_username', $authorUsername);
51
        }
52
53 6
        if ($favoritedByUsername) {
54 2
            $qb->innerJoin('a.favoritedBy', 'favoritedBy');
55 2
            $qb->andWhere('favoritedBy.username = :favoritedby_username');
56 2
            $qb->setParameter('favoritedby_username', $favoritedByUsername);
57
        }
58
59 6
        return $qb;
60
    }
61
62
    /**
63
     * @param null|string $tag
64
     * @param null|string $authorUsername
65
     * @param null|string $favoritedByUsername
66
     *
67
     * @return int
68
     */
69 6
    public function getArticlesListCount(?string $tag, ?string $authorUsername, ?string $favoritedByUsername)
70
    {
71
        try {
72
            return (int) $this
73 6
                ->getArticlesListQueryBuilder($tag, $authorUsername, $favoritedByUsername)
74 6
                ->select('count(a.id) as total')
75 6
                ->getQuery()
76 6
                ->getSingleScalarResult();
77
        } catch (NonUniqueResultException $e) {
78
            return 0;
79
        }
80
    }
81
82
    /**
83
     * @param int         $offset
84
     * @param int         $limit
85
     * @param null|string $tag
86
     * @param null|string $authorUsername
87
     * @param null|string $favoritedByUsername
88
     *
89
     * @return Article[]
90
     */
91 6
    public function getArticlesList(int $offset, int $limit, ?string $tag, ?string $authorUsername, ?string $favoritedByUsername)
92
    {
93
        return $this
94 6
            ->getArticlesListQueryBuilder($tag, $authorUsername, $favoritedByUsername)
95 6
            ->setFirstResult($offset)
96 6
            ->setMaxResults($limit)
97 6
            ->getQuery()
98 6
            ->getResult();
99
    }
100
101
    /**
102
     * @param User $user
103
     *
104
     * @return QueryBuilder
105
     */
106 3
    private function getArticlesFeedQueryBuilder(User $user)
107
    {
108
        return $this
109 3
            ->createQueryBuilder('a')
110 3
            ->innerJoin('a.author', 'author')
111 3
            ->andWhere('author IN (:authors_ids)')
112 3
            ->setParameter('authors_ids', $user->getFolloweds())
113 3
            ->orderBy('a.id', 'desc');
114
    }
115
116
    /**
117
     * @param User $user
118
     *
119
     * @return int
120
     */
121 3
    public function getArticlesFeedCount(User $user)
122
    {
123
        try {
124
            return (int) $this
125 3
                ->getArticlesFeedQueryBuilder($user)
126 3
                ->select('count(a.id) as total')
127 3
                ->getQuery()
128 3
                ->getSingleScalarResult();
129
        } catch (NonUniqueResultException $e) {
130
            return 0;
131
        }
132
    }
133
134
    /**
135
     * @param User $user
136
     * @param int  $offset
137
     * @param int  $limit
138
     *
139
     * @return Article[]
140
     */
141 3
    public function getArticlesFeed(User $user, int $offset, int $limit)
142
    {
143
        return $this
144 3
            ->getArticlesFeedQueryBuilder($user)
145 3
            ->setFirstResult($offset)
146 3
            ->setMaxResults($limit)
147 3
            ->getQuery()
148 3
            ->getResult();
149
    }
150
}
151