Passed
Push — master ( bd04f5...730552 )
by
unknown
15:53 queued 07:26
created

CNotebookRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 36
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findByUser() 0 26 2
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Session;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Repository\ResourceRepository;
13
use Chamilo\CourseBundle\Entity\CNotebook;
14
use Doctrine\Persistence\ManagerRegistry;
15
16
/**
17
 * @extends ResourceRepository<CNotebook>
18
 */
19
class CNotebookRepository extends ResourceRepository
20
{
21
    public function __construct(ManagerRegistry $registry)
22
    {
23
        parent::__construct($registry, CNotebook::class);
24
    }
25
26
    /**
27
     * Get the user notebooks in a course.
28
     */
29
    public function findByUser(
30
        User $user,
31
        Course $course,
32
        ?Session $session = null,
33
        string $orderField = 'creation_date',
34
        string $orderDirection = 'DESC'
35
    ): array {
36
        $qb = $this->getResourcesByCourse($course, $session);
37
38
        $alias = $qb->getRootAliases()[0] ?? 'n';
39
40
        $map = [
41
            'creation_date' => 'creationDate',
42
            'update_date'   => 'updateDate',
43
            'title'         => 'title',
44
        ];
45
        $prop = $map[$orderField] ?? 'creationDate';
46
47
        $qb->andWhere($qb->expr()->eq($alias.'.user', ':owner'))
48
            ->setParameter('owner', $user);
49
50
        $direction = strtoupper($orderDirection) === 'ASC' ? 'ASC' : 'DESC';
51
52
        $qb->orderBy($alias.'.'.$prop, $direction);
53
54
        return $qb->getQuery()->getResult();
55
    }
56
}
57