Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
17:36 queued 08:49
created

PortfolioCommentRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

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

2 Methods

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