Passed
Push — master ( 31b9cb...154d5e )
by Nicolas
13:25
created

ArticleRepository::getArticlesList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 5
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 1
rs 10
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\ORM\NonUniqueResultException;
11
use Doctrine\ORM\NoResultException;
12
use Doctrine\ORM\QueryBuilder;
13
use Doctrine\Persistence\ManagerRegistry;
14
15
final class ArticleRepository extends ServiceEntityRepository
16
{
17 27
    public function __construct(ManagerRegistry $registry)
18
    {
19 27
        parent::__construct($registry, Article::class);
20
    }
21
22 6
    public function getArticlesListCount(?string $tag, ?string $authorUsername, ?string $favoritedByUsername): int
23
    {
24
        try {
25
            return (int) $this
26 6
                ->getArticlesListQueryBuilder($tag, $authorUsername, $favoritedByUsername)
27 6
                ->select('count(a.id) as total')
28 6
                ->getQuery()
29 6
                ->getSingleScalarResult()
30
            ;
31
        } catch (NonUniqueResultException | NoResultException $exception) {
32
            return 0;
33
        }
34
    }
35
36
    /**
37
     * @return Article[]
38
     */
39 6
    public function getArticlesList(
40
        int $offset,
41
        int $limit,
42
        ?string $tag,
43
        ?string $authorUsername,
44
        ?string $favoritedByUsername
45
    ): array {
46
        return $this
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getArticle...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
47 6
            ->getArticlesListQueryBuilder($tag, $authorUsername, $favoritedByUsername)
48 6
            ->setFirstResult($offset)
49 6
            ->setMaxResults($limit)
50 6
            ->getQuery()
51 6
            ->getResult()
52
        ;
53
    }
54
55 3
    public function getArticlesFeedCount(User $user): int
56
    {
57
        try {
58
            return (int) $this
59 3
                ->getArticlesFeedQueryBuilder($user)
60 3
                ->select('count(a.id) as total')
61 3
                ->getQuery()
62 3
                ->getSingleScalarResult()
63
            ;
64
        } catch (NonUniqueResultException | NoResultException $exception) {
65
            return 0;
66
        }
67
    }
68
69
    /**
70
     * @return Article[]
71
     */
72 3
    public function getArticlesFeed(User $user, int $offset, int $limit): array
73
    {
74
        return $this
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getArticle...getQuery()->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
75 3
            ->getArticlesFeedQueryBuilder($user)
76 3
            ->setFirstResult($offset)
77 3
            ->setMaxResults($limit)
78 3
            ->getQuery()
79 3
            ->getResult()
80
        ;
81
    }
82
83 6
    private function getArticlesListQueryBuilder(
84
        ?string $tag,
85
        ?string $authorUsername,
86
        ?string $favoritedByUsername
87
    ): QueryBuilder {
88
        $queryBuilder = $this
89 6
            ->createQueryBuilder('a')
90 6
            ->innerJoin('a.author', 'author')
91 6
            ->orderBy('a.id', 'desc')
92
        ;
93
94 6
        if ($tag) {
95 3
            $queryBuilder->innerJoin('a.tags', 't');
96 3
            $queryBuilder->andWhere('t.name = :tag');
97 3
            $queryBuilder->setParameter('tag', $tag);
98
        }
99
100 6
        if ($authorUsername) {
101 2
            $queryBuilder->andWhere('author.username = :author_username');
102 2
            $queryBuilder->setParameter('author_username', $authorUsername);
103
        }
104
105 6
        if ($favoritedByUsername) {
106 2
            $queryBuilder->innerJoin('a.favoritedBy', 'favoritedBy');
107 2
            $queryBuilder->andWhere('favoritedBy.username = :favoritedby_username');
108 2
            $queryBuilder->setParameter('favoritedby_username', $favoritedByUsername);
109
        }
110
111 6
        return $queryBuilder;
112
    }
113
114 3
    private function getArticlesFeedQueryBuilder(User $user): QueryBuilder
115
    {
116
        return $this
117 3
            ->createQueryBuilder('a')
118 3
            ->innerJoin('a.author', 'author')
119 3
            ->andWhere('author IN (:authors_ids)')
120 3
            ->setParameter('authors_ids', $user->getFolloweds())
121 3
            ->orderBy('a.id', 'desc')
122
        ;
123
    }
124
}
125