Passed
Push — master ( c83d6c...5bd3d9 )
by Nicolas
05:49
created

ArticleRepository::getArticlesListQueryBuilder()   A

Complexity

Conditions 4
Paths 8

Size

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