Test Setup Failed
Push — master ( faa7b9...b8668c )
by Alexey
02:33
created

PostRepository::createPublicFeedPostsQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 9
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Repository\Blogs;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\QueryBuilder;
7
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Post;
8
9
class PostRepository extends EntityRepository
10 1
{
11
    public function add(Post $entity): void
12
    {
13 1
        $this->getEntityManager()->persist($entity);
14
    }
15 1
16 1
    public function getPostWithComments(string $postId): ?Post
17 1
    {
18 1
        /** @var QueryBuilder $qb */
19 1
        $qb = $this->createQueryBuilder('p');
20 1
21 1
        return $qb
22
            ->select(['p', 'c', 'a'])
23
            ->leftJoin('p.comments', 'c')
24
            // @todo Optimize https://ocramius.github.io/blog/doctrine-orm-optimization-hydration/
25
            ->leftJoin('c.author', 'a')
26
            ->where($qb->expr()->eq('p.id', ':post_id'))
27
            ->orderBy('c.number', 'asc')
28
            ->setParameter('post_id', $postId)
29
            ->getQuery()->getOneOrNullResult()
30
        ;
31
    }
32
33
    public function createPublicFeedPostsQuery(): QueryBuilder
34
    {
35
        $qb = $this->createQueryBuilder('p');
36
37
        return $qb
38
            // @todo optimize hydration
39
            ->select(['p', 'pa', 'pt', 'pf'])
40
            ->innerJoin('p.author', 'pa')
41
            ->leftJoin('p.postTags', 'pt')
42
            ->leftJoin('p.files', 'pf')
43
            ->where('p.private = FALSE')
44
            ->andWhere('pa.public = TRUE')
45
        ;
46
    }
47
}