Passed
Push — master ( 8f8784...de3d4b )
by Julito
11:32
created

SessionRepository::addUserInCourse()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 8
nop 4
dl 0
loc 37
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Repository;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Chamilo\CoreBundle\Entity\User;
10
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
11
use Doctrine\Common\Persistence\ManagerRegistry;
12
13
/**
14
 * SessionRepository.
15
 *
16
 * @author  Julio Montoya <[email protected]>
17
 */
18
class SessionRepository extends ServiceEntityRepository
19
{
20
    /**
21
     * SessionRepository constructor.
22
     */
23
    public function __construct(ManagerRegistry $registry)
24
    {
25
        parent::__construct($registry, Session::class);
26
    }
27
28
    /**
29
     * @param $status
30
     */
31
    protected function addUserInCourse(
32
        int $status,
33
        User $user,
34
        Course $course,
35
        Session $session
36
    ) {
37
        if ($session->isActive() &&
38
            $user->getIsActive() &&
39
            $course->isActive()
40
        ) {
41
            if ($session->hasCourse($course)) {
42
                switch ($status) {
43
                    case Session::DRH:
44
                        if ($user->hasRole('ROLE_RRHH')) {
45
                            $session->addUserInSession(Session::DRH, $user);
46
                        }
47
48
                        break;
49
                    case Session::STUDENT:
50
                        $session->addUserInSession(Session::STUDENT, $user);
51
                        $session->addUserInCourse(
52
                            Session::STUDENT,
53
                            $user,
54
                            $course
55
                        );
56
57
                        break;
58
                    case Session::COACH:
59
                        if ($user->hasRole('ROLE_TEACHER')) {
60
                            $session->addUserInCourse(
61
                                Session::COACH,
62
                                $user,
63
                                $course
64
                            );
65
                        }
66
67
                        break;
68
                }
69
            }
70
        }
71
    }
72
}
73