Passed
Branch develop (21ab80)
by Nicolas
05:15
created

ArticleRepository::getArticles()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 8
nop 5
dl 0
loc 28
ccs 18
cts 18
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 Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * @method Article|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method Article|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method Article[]    findAll()
14
 * @method Article[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class ArticleRepository extends ServiceEntityRepository
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 22
    public function __construct(RegistryInterface $registry)
22
    {
23 22
        parent::__construct($registry, Article::class);
24 22
    }
25
26
    /**
27
     * @param int         $offset
28
     * @param int         $limit
29
     * @param null|string $tag
30
     * @param null|string $author
31
     * @param null|string $favorited
32
     *
33
     * @return Article[]
34
     */
35 5
    public function getArticles(int $offset, int $limit, ?string $tag, ?string $author, ?string $favorited)
36
    {
37
        $qb = $this
38 5
            ->createQueryBuilder('a')
39 5
            ->innerJoin('a.author', 'author')
40 5
            ->orderBy('a.id', 'desc')
41 5
            ->setFirstResult($offset)
42 5
            ->setMaxResults($limit)
43
        ;
44
45 5
        if ($tag) {
46 2
            $qb->innerJoin('a.tags', 't');
47 2
            $qb->andWhere('t.name = :tag');
48 2
            $qb->setParameter('tag', $tag);
49
        }
50
51 5
        if ($author) {
52 2
            $qb->andWhere('author.username = :author_username');
53 2
            $qb->setParameter('author_username', $author);
54
        }
55
56 5
        if ($favorited) {
57 2
            $qb->innerJoin('a.favoritedBy', 'favoritedBy');
58 2
            $qb->andWhere('favoritedBy.username = :favoritedby_username');
59 2
            $qb->setParameter('favoritedby_username', $favorited);
60
        }
61
62 5
        return $qb->getQuery()->getResult();
63
    }
64
65
    /**
66
     * @param User $user
67
     * @param int  $offset
68
     * @param int  $limit
69
     *
70
     * @return Article[]
71
     */
72 2
    public function getFollowedUsersArticles(User $user, int $offset, int $limit)
73
    {
74
        return $this
75 2
            ->createQueryBuilder('a')
76 2
            ->innerJoin('a.author', 'author')
77 2
            ->andWhere('author IN (:authors_ids)')
78 2
            ->setParameter('authors_ids', $user->getFolloweds())
79 2
            ->orderBy('a.id', 'desc')
80 2
            ->setFirstResult($offset)
81 2
            ->setMaxResults($limit)
82 2
            ->getQuery()
83 2
            ->getResult();
84
    }
85
}
86