Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
10:09
created

PortfolioCommentRepository::findCommentsByUser()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 29
rs 9.3222
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Repository\Node;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\PortfolioComment;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Repository\ResourceRepository;
12
use Chamilo\CoreBundle\Traits\Repository\ORM\NestedTreeRepositoryTrait;
13
use Doctrine\Persistence\ManagerRegistry;
14
15
class PortfolioCommentRepository extends ResourceRepository
16
{
17
    use NestedTreeRepositoryTrait;
18
19
    public function __construct(ManagerRegistry $registry)
20
    {
21
        parent::__construct($registry, PortfolioComment::class);
22
    }
23
24
    public function findCommentsByUser(User $user, ?Course $course, ?Session $session, ?array $orderBy = null): array
25
    {
26
        $qbComments = $this->createQueryBuilder('comment');
27
        $qbComments
28
            ->where('comment.author = :owner')
29
            ->setParameter('owner', $user);
30
31
        if ($course) {
32
            $qbComments
33
                ->join('comment.item', 'item')
34
                ->andWhere('item.course = :course')
35
                ->setParameter('course', $course);
36
37
            if ($session) {
38
                $qbComments
39
                    ->andWhere('item.session = :session')
40
                    ->setParameter('session', $session);
41
            } else {
42
                $qbComments->andWhere('item.session IS NULL');
43
            }
44
        }
45
46
        if ($orderBy) {
47
            foreach ($orderBy as $sort => $order) {
48
                $qbComments->addOrderBy($sort, $order);
49
            }
50
        }
51
52
        return $qbComments->getQuery()->getResult();
53
    }
54
}
55