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

PortfolioCommentRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A findCommentsByUser() 0 29 5
A __construct() 0 3 1
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