Passed
Pull Request — master (#6187)
by
unknown
09:13
created

CreateSessionWithUsersAndCoursesAction::__invoke()   C

Complexity

Conditions 11
Paths 91

Size

Total Lines 103
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 72
c 1
b 0
f 0
nc 91
nop 1
dl 0
loc 103
rs 6.4642

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\Controller\Api;
8
9
use ApiPlatform\Validator\ValidatorInterface;
10
use Chamilo\CoreBundle\Dto\CreateSessionWithUsersAndCoursesInput;
11
use Chamilo\CoreBundle\Entity\Session;
12
use Chamilo\CoreBundle\Entity\SessionCategory;
13
use Chamilo\CoreBundle\Entity\SessionRelCourse;
14
use Chamilo\CoreBundle\Entity\SessionRelUser;
15
use Chamilo\CoreBundle\Entity\SessionRelCourseRelUser;
16
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
17
use Chamilo\CoreBundle\Repository\Node\UserRepository;
18
use Doctrine\ORM\EntityManagerInterface;
19
use Symfony\Component\HttpKernel\Attribute\AsController;
20
21
#[AsController]
22
class CreateSessionWithUsersAndCoursesAction
23
{
24
    public function __construct(
25
        private EntityManagerInterface $em,
26
        private ValidatorInterface $validator,
27
        private UserRepository $userRepo,
28
        private CourseRepository $courseRepo
29
    ) {}
30
31
    public function __invoke(CreateSessionWithUsersAndCoursesInput $data): Session
32
    {
33
        $this->validator->validate($data);
34
35
        $session = new Session();
36
        $session
37
            ->setTitle($data->getTitle())
38
            ->setDescription($data->getDescription() ?? '')
39
            ->setVisibility($data->getVisibility() ?? 1)
40
            ->setNbrCourses(count($data->getCourseIds()))
41
            ->setNbrUsers(count($data->getStudentIds()) + count($data->getTutorIds()))
42
            ->setShowDescription($data->getShowDescription() ?? false)
43
            ->setDuration($data->getDuration() ?? 0)
44
            ->setDisplayStartDate($data->getDisplayStartDate() ?? new \DateTime())
45
            ->setDisplayEndDate($data->getDisplayEndDate() ?? new \DateTime())
46
            ->setAccessStartDate($data->getAccessStartDate() ?? new \DateTime())
47
            ->setAccessEndDate($data->getAccessEndDate() ?? new \DateTime())
48
            ->setCoachAccessStartDate($data->getCoachAccessStartDate() ?? new \DateTime())
49
            ->setCoachAccessEndDate($data->getCoachAccessEndDate() ?? new \DateTime())
50
            ->setValidityInDays($data->getValidityInDays() ?? 0);
51
52
        if ($data->getCategory()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data->getCategory() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
53
            $category = $this->em->getRepository(SessionCategory::class)->find($data->getCategory());
54
            if (!$category) {
55
                throw new \RuntimeException("Invalid category ID: " . $data->getCategory());
56
            }
57
            $session->setCategory($category);
58
        }
59
60
        $this->em->persist($session);
61
62
        $relCourses = [];
63
        $courses = [];
64
        foreach ($data->getCourseIds() as $courseId) {
65
            $course = $this->courseRepo->find($courseId);
66
            if (!$course) {
67
                continue;
68
            }
69
70
            $courses[$courseId] = $course;
71
72
            $relCourse = new SessionRelCourse();
73
            $relCourse->setSession($session);
74
            $relCourse->setCourse($course);
75
            $relCourse->setNbrUsers(0);
76
            $this->em->persist($relCourse);
77
78
            $relCourses[$courseId] = $relCourse;
79
        }
80
81
        foreach ($data->getStudentIds() as $userId) {
82
            $user = $this->userRepo->find($userId);
83
            if (!$user) {
84
                continue;
85
            }
86
87
            $rel = new SessionRelUser();
88
            $rel
89
                ->setSession($session)
90
                ->setUser($user)
91
                ->setRelationType(Session::STUDENT);
92
93
            $this->em->persist($rel);
94
95
            foreach ($courses as $courseId => $course) {
96
                $relCourseUser = new SessionRelCourseRelUser();
97
                $relCourseUser
98
                    ->setSession($session)
99
                    ->setUser($user)
100
                    ->setCourse($course)
101
                    ->setStatus(Session::STUDENT)
102
                    ->setVisibility(1)
103
                    ->setProgress(0)
104
                    ->setLegalAgreement(0);
105
106
                $this->em->persist($relCourseUser);
107
108
                if (isset($relCourses[$courseId])) {
109
                    $relCourses[$courseId]->setNbrUsers(
110
                        $relCourses[$courseId]->getNbrUsers() + 1
111
                    );
112
                }
113
            }
114
        }
115
116
        foreach ($data->getTutorIds() as $userId) {
117
            $user = $this->userRepo->find($userId);
118
            if (!$user) {
119
                continue;
120
            }
121
122
            $rel = new SessionRelUser();
123
            $rel
124
                ->setSession($session)
125
                ->setUser($user)
126
                ->setRelationType(Session::SESSION_ADMIN);
127
128
            $this->em->persist($rel);
129
        }
130
131
        $this->em->flush();
132
133
        return $session;
134
    }
135
}
136