Passed
Push — master ( 3fcb32...49d6fc )
by Julito
09:27
created

CGroupRepository::addStatusQueryBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 12
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 findOneByCode(string $code): ?CGroup
40
    {
41
        return $this->findOneBy(
42
            [
43
                'code' => $code,
44
            ]
45
        );
46
    }
47
48
    public function findOneByTitle(string $name): ?CGroup
49
    {
50
        return $this->findOneBy(
51
            [
52
                'name' => $name,
53
            ]
54
        );
55
    }
56
57
    private function addStatusQueryBuilder(?int $status = null, QueryBuilder $qb = null): QueryBuilder
58
    {
59
        $qb = $this->getOrCreateQueryBuilder($qb);
60
61
        if (null !== $status) {
62
            $qb
63
                ->andWhere('resource.status = :status')
64
                ->setParameter('status', $status)
65
            ;
66
        }
67
68
        return $qb;
69
    }
70
71
    private function addCategoryQueryBuilder(?int $categoryId = null, QueryBuilder $qb = null): QueryBuilder
72
    {
73
        $qb = $this->getOrCreateQueryBuilder($qb);
74
75
        if (null === $categoryId) {
76
            $qb
77
                ->andWhere('resource.category is NULL')
78
            ;
79
        } else {
80
            $qb
81
                ->andWhere('resource.category = :category')
82
                ->setParameter('category', $categoryId)
83
            ;
84
        }
85
86
        return $qb;
87
    }
88
}
89