| Conditions | 10 |
| Paths | 54 |
| Total Lines | 80 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | public function __invoke(CreateSessionWithUsersAndCoursesInput $data): Session |
||
| 31 | { |
||
| 32 | $this->validator->validate($data); |
||
| 33 | |||
| 34 | $session = new Session(); |
||
| 35 | $session |
||
| 36 | ->setTitle($data->getTitle()) |
||
| 37 | ->setDescription($data->getDescription() ?? '') |
||
| 38 | ->setVisibility($data->getVisibility() ?? 1) |
||
| 39 | ->setNbrCourses(count($data->getCourseIds())) |
||
| 40 | ->setNbrUsers(count($data->getStudentIds()) + count($data->getTutorIds())); |
||
| 41 | |||
| 42 | $this->em->persist($session); |
||
| 43 | |||
| 44 | $relCourses = []; |
||
| 45 | foreach ($data->getCourseIds() as $courseId) { |
||
| 46 | $course = $this->courseRepo->find($courseId); |
||
| 47 | if (!$course) continue; |
||
| 48 | |||
| 49 | $relCourse = new SessionRelCourse(); |
||
| 50 | $relCourse->setSession($session); |
||
| 51 | $relCourse->setCourse($course); |
||
| 52 | $relCourse->setNbrUsers(0); |
||
| 53 | $this->em->persist($relCourse); |
||
| 54 | |||
| 55 | $relCourses[$courseId] = $relCourse; |
||
| 56 | } |
||
| 57 | |||
| 58 | foreach ($data->getStudentIds() as $userId) { |
||
| 59 | $user = $this->userRepo->find($userId); |
||
| 60 | if (!$user) continue; |
||
| 61 | |||
| 62 | $rel = new SessionRelUser(); |
||
| 63 | $rel |
||
| 64 | ->setSession($session) |
||
| 65 | ->setUser($user) |
||
| 66 | ->setRelationType(Session::STUDENT); |
||
| 67 | |||
| 68 | $this->em->persist($rel); |
||
| 69 | |||
| 70 | foreach ($data->getCourseIds() as $courseId) { |
||
| 71 | $course = $this->courseRepo->find($courseId); |
||
| 72 | if (!$course) continue; |
||
| 73 | |||
| 74 | $relCourseUser = new SessionRelCourseRelUser(); |
||
| 75 | $relCourseUser |
||
| 76 | ->setSession($session) |
||
| 77 | ->setUser($user) |
||
| 78 | ->setCourse($course) |
||
| 79 | ->setStatus(Session::STUDENT) |
||
| 80 | ->setVisibility(1) |
||
| 81 | ->setProgress(0) |
||
| 82 | ->setLegalAgreement(0); |
||
| 83 | |||
| 84 | $this->em->persist($relCourseUser); |
||
| 85 | |||
| 86 | if (isset($relCourses[$courseId])) { |
||
| 87 | $relCourses[$courseId]->setNbrUsers( |
||
| 88 | $relCourses[$courseId]->getNbrUsers() + 1 |
||
| 89 | ); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | foreach ($data->getTutorIds() as $userId) { |
||
| 95 | $user = $this->userRepo->find($userId); |
||
| 96 | if (!$user) continue; |
||
| 97 | |||
| 98 | $rel = new SessionRelUser(); |
||
| 99 | $rel |
||
| 100 | ->setSession($session) |
||
| 101 | ->setUser($user) |
||
| 102 | ->setRelationType(Session::SESSION_ADMIN); |
||
| 103 | |||
| 104 | $this->em->persist($rel); |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->em->flush(); |
||
| 108 | |||
| 109 | return $session; |
||
| 110 | } |
||
| 112 |