Passed
Push — master ( 3ac343...373e44 )
by Angel Fernando Quiroz
08:23 queued 15s
created

IsAllowedToEditHelper::check()   F

Complexity

Conditions 30
Paths 2563

Size

Total Lines 81
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 30
eloc 41
nc 2563
nop 6
dl 0
loc 81
rs 0
c 1
b 0
f 0

How to fix   Long Method    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
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\ServiceHelper;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Entity\Session;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CoreBundle\Settings\SettingsManager;
13
use ExtraFieldValue;
14
use Symfony\Bundle\SecurityBundle\Security;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
17
readonly class IsAllowedToEditHelper
18
{
19
    public function __construct(
20
        private SettingsManager $settingsManager,
21
        private Security $security,
22
        private RequestStack $requestStack,
23
        private CidReqHelper $cidReqHelper,
24
    ) {
25
    }
26
27
    public function check(
28
        bool $tutor = false,
29
        bool $coach = false,
30
        bool $sessionCoach = false,
31
        bool $checkStudentView = true,
32
        ?Course $course = null,
33
        ?Session $session = null,
34
    ): bool {
35
        /** @var User $user */
36
        $user = $this->security->getUser();
37
38
        $studentViewIsActive = 'studentview' === $this->requestStack->getSession()->get('studentview');
39
40
        $isSessionAdminAllowedToEdit = 'true' === $this->settingsManager->getSetting('session.session_admins_edit_courses_content');
41
42
        if ($user->isAdmin() || ($user->isSessionAdmin() && $isSessionAdminAllowedToEdit)) {
43
            if ($checkStudentView && $studentViewIsActive) {
44
                return false;
45
            }
46
47
            return true;
48
        }
49
50
        $session = $session ?: $this->cidReqHelper->getSessionEntity();
51
        $course = $course ?: $this->cidReqHelper->getCourseEntity();
52
53
        if ($session && $course && 'true' === $this->settingsManager->getSetting('session.session_courses_read_only_mode')) {
54
            $lockExrafieldField = (new ExtraFieldValue('course'))
55
                ->get_values_by_handler_and_field_variable(
56
                    $course->getId(),
57
                    'session_courses_read_only_mode'
58
                );
59
60
            if (!empty($lockExrafieldField['value'])) {
61
                return false;
62
            }
63
        }
64
65
        $isCoachAllowedToEdit = $session?->hasCoach($user) && !$studentViewIsActive;
66
        $sessionVisibility = $session?->setAccessVisibilityByUser($user);
67
        $isCourseAdmin = $user->hasRole('ROLE_CURRENT_COURSE_TEACHER') || $user->hasRole('ROLE_CURRENT_COURSE_SESSION_TEACHER');
68
69
        if (!$isCourseAdmin && $tutor) {
70
            $isCourseAdmin = $user->isCourseTutor($course, $session);
71
        }
72
73
        if (!$isCourseAdmin && $coach) {
74
            if (Session::READ_ONLY === $sessionVisibility) {
75
                $isCoachAllowedToEdit = false;
76
            }
77
78
            if ('true' === $this->settingsManager->getSetting('session.allow_coach_to_edit_course_session')) {
79
                $isCourseAdmin = $isCoachAllowedToEdit;
80
            }
81
        }
82
83
        if (!$isCourseAdmin && $sessionCoach) {
84
            $isCourseAdmin = $isCoachAllowedToEdit;
85
        }
86
87
        if ('true' !== $this->settingsManager->getSetting('course.student_view_enabled')) {
88
            return $isCourseAdmin;
89
        }
90
91
        if ($session) {
92
            if (Session::READ_ONLY === $sessionVisibility) {
93
                $isCoachAllowedToEdit = false;
94
            }
95
96
            $isAllowed = 'true' === $this->settingsManager->getSetting('session.allow_coach_to_edit_course_session') && $isCoachAllowedToEdit;
97
98
            if ($checkStudentView) {
99
                $isAllowed = $isAllowed && !$studentViewIsActive;
100
            }
101
        } elseif ($checkStudentView) {
102
            $isAllowed = $isCourseAdmin && !$studentViewIsActive;
103
        } else {
104
            $isAllowed = $isCourseAdmin;
105
        }
106
107
        return $isAllowed;
108
    }
109
}