Passed
Push — master ( 258428...ddae1a )
by Julito
11:33
created

CGroupRepository::findOneByCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Repository\ResourceRepository;
12
use Chamilo\CourseBundle\Entity\CGroup;
13
use Doctrine\ORM\QueryBuilder;
14
use Doctrine\Persistence\ManagerRegistry;
15
16
final class CGroupRepository extends ResourceRepository
17
{
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        parent::__construct($registry, CGroup::class);
21
    }
22
23
    public function findAllByCourse(
24
        Course $course,
25
        Session $session = null,
26
        ?string $title = null,
27
        ?int $status = null,
28
        ?int $categoryId = null
29
    ): QueryBuilder {
30
        $qb = $this->getResourcesByCourse($course, $session);
31
32
        $this->addStatusQueryBuilder($status, $qb);
33
        $this->addCategoryQueryBuilder($categoryId, $qb);
34
        $this->addTitleQueryBuilder($title, $qb);
35
36
        return $qb;
37
    }
38
39
    public function findOneByTitle(string $name): ?CGroup
40
    {
41
        return $this->findOneBy(
42
            [
43
                'name' => $name,
44
            ]
45
        );
46
    }
47
48
    private function addStatusQueryBuilder(?int $status = null, QueryBuilder $qb = null): QueryBuilder
49
    {
50
        $qb = $this->getOrCreateQueryBuilder($qb);
51
52
        if (null !== $status) {
53
            $qb
54
                ->andWhere('resource.status = :status')
55
                ->setParameter('status', $status)
56
            ;
57
        }
58
59
        return $qb;
60
    }
61
62
    private function addCategoryQueryBuilder(?int $categoryId = null, QueryBuilder $qb = null): QueryBuilder
63
    {
64
        $qb = $this->getOrCreateQueryBuilder($qb);
65
66
        if (null === $categoryId) {
67
            $qb
68
                ->andWhere('resource.category is NULL')
69
            ;
70
        } else {
71
            $qb
72
                ->andWhere('resource.category = :category')
73
                ->setParameter('category', $categoryId)
74
            ;
75
        }
76
77
        return $qb;
78
    }
79
}
80