| Conditions | 7 |
| Paths | 10 |
| Total Lines | 103 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 40 | public function __invoke(CreateUserOnAccessUrlInput $data): User |
||
| 41 | { |
||
| 42 | $this->validator->validate($data); |
||
| 43 | |||
| 44 | $url = $this->accessUrlRepo->find($data->getAccessUrlId()); |
||
| 45 | if (!$url) { |
||
| 46 | throw new NotFoundHttpException('Access URL not found.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | $user = new User(); |
||
| 50 | $user |
||
| 51 | ->setUsername($data->getUsername()) |
||
| 52 | ->setFirstname($data->getFirstname()) |
||
| 53 | ->setLastname($data->getLastname()) |
||
| 54 | ->setEmail($data->getEmail()) |
||
| 55 | ->setLocale($data->getLocale() ?? 'en') |
||
| 56 | ->setTimezone($data->getTimezone() ?? 'Europe/Paris') |
||
| 57 | ->setStatus($data->getStatus() ?? 5) |
||
| 58 | ->setActive(User::ACTIVE) |
||
| 59 | ->setPassword( |
||
| 60 | $this->passwordHasher->hashPassword($user, $data->getPassword()) |
||
| 61 | ) |
||
| 62 | ; |
||
| 63 | |||
| 64 | $this->em->persist($user); |
||
| 65 | $this->em->flush(); |
||
| 66 | |||
| 67 | if (!empty($data->extraFields)) { |
||
| 68 | foreach ($data->extraFields as $variable => $value) { |
||
| 69 | $extraField = $this->extraFieldRepo->findOneBy([ |
||
| 70 | 'variable' => $variable, |
||
| 71 | 'itemType' => 1, |
||
| 72 | ]); |
||
| 73 | |||
| 74 | if (!$extraField) { |
||
| 75 | throw new RuntimeException("ExtraField '{$variable}' not found for users."); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->extraFieldValuesRepo->updateItemData( |
||
| 79 | $extraField, |
||
| 80 | $user, |
||
| 81 | $value |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | $hasAccess = $user->getPortals()->exists( |
||
| 87 | fn ($k, $rel) => $rel->getUrl()?->getId() === $url->getId() |
||
| 88 | ); |
||
| 89 | |||
| 90 | if (!$hasAccess) { |
||
| 91 | $rel = new AccessUrlRelUser(); |
||
| 92 | $rel->setUser($user)->setUrl($url); |
||
| 93 | |||
| 94 | $this->em->persist($rel); |
||
| 95 | $this->em->flush(); |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($data->getSendEmail()) { |
||
| 99 | $subject = $this->translator->trans('You have been enrolled in a new course'); |
||
| 100 | |||
| 101 | $sessionUrl = '/sessions'; |
||
| 102 | $password = $data->getPassword(); |
||
| 103 | |||
| 104 | $body = $this->translator->trans( |
||
| 105 | 'Hello %s,<br><br>'. |
||
| 106 | 'You have been enrolled in the Chamilo platform.<br>'. |
||
| 107 | 'You can access your account from <a href="%s">here</a>.<br><br>'. |
||
| 108 | 'Your login credentials are:<br>'. |
||
| 109 | 'Username: <strong>%s</strong><br>'. |
||
| 110 | 'Password: <strong>%s</strong><br><br>'. |
||
| 111 | 'Best regards,<br>'. |
||
| 112 | 'Chamilo' |
||
| 113 | ); |
||
| 114 | |||
| 115 | $body = \sprintf( |
||
| 116 | $body, |
||
| 117 | $user->getFullname(), |
||
| 118 | $sessionUrl, |
||
| 119 | $user->getUsername(), |
||
| 120 | $password |
||
| 121 | ); |
||
| 122 | |||
| 123 | $currentUser = $this->userHelper->getCurrent(); |
||
| 124 | $senderId = $currentUser?->getId() ?? 1; |
||
| 125 | |||
| 126 | $this->messageHelper->sendMessage( |
||
| 127 | $user->getId(), |
||
| 128 | $subject, |
||
| 129 | $body, |
||
| 130 | [], |
||
| 131 | [], |
||
| 132 | 0, |
||
| 133 | 0, |
||
| 134 | 0, |
||
| 135 | $senderId, |
||
| 136 | 0, |
||
| 137 | false, |
||
| 138 | true |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $user; |
||
| 143 | } |
||
| 145 |