| Conditions | 11 |
| Paths | 28 |
| Total Lines | 66 |
| Code Lines | 43 |
| 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 |
||
| 70 | public function process(Request $request) |
||
| 71 | { |
||
| 72 | $output = ['success' => false, 'already_invited' => false, 'already_a_member' => false, 'hit_limit' => false]; |
||
| 73 | |||
| 74 | $formType = $this->formFactory->create(get_class($this->userInviteType)); |
||
| 75 | $requestHander = $this->requestHandlerManager->getRequestHandler($request); |
||
| 76 | |||
| 77 | if ($request->isMethod('POST')) { |
||
| 78 | $requestHander->handleForm($formType, $request); |
||
| 79 | if ($formType->isSubmitted() && $formType->isValid()) { |
||
| 80 | $email = $formType->getData()['email']; |
||
| 81 | $role = $formType->getData()['role'] ?? $this->defaultRole; |
||
| 82 | $output['processed'] = true; |
||
| 83 | try { |
||
| 84 | $user = $this->security->getUser(); |
||
| 85 | if (!$user instanceof UserInterface) { |
||
| 86 | throw new \InvalidArgumentException('Not a user'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (!$user instanceof MemberInterface) { |
||
| 90 | $this->getLogger()->critical('A user tried to send a team invite when not a member of a team'); |
||
| 91 | throw new \InvalidArgumentException('Not a member of a team'); |
||
| 92 | } |
||
| 93 | $team = $this->teamRepository->getByMember($user); |
||
| 94 | |||
| 95 | if ($this->inviteCodeRepository->hasInviteForEmailAndTeam($email, $team)) { |
||
| 96 | throw new AlreadyInvitedException(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!in_array($role, $this->roles) && $this->defaultRole !== $role) { |
||
| 100 | $this->getLogger()->error('A team invite was not sent due an invalid role being used', ['role' => $role]); |
||
| 101 | throw new GeneralException('Invalid role'); |
||
| 102 | } |
||
| 103 | |||
| 104 | $inviteCode = $this->entityFactory->buildTeamInviteCode($user, $team, $email, $role); |
||
| 105 | if (!$this->authorizationChecker->isGranted(TeamInviteCode::AUTH_CHECKER_ATTRIBUTE, $inviteCode)) { |
||
| 106 | $output['success'] = false; |
||
| 107 | $output['hit_limit'] = true; |
||
| 108 | $this->getLogger()->info('A team invite was not sent due to plan limits'); |
||
| 109 | |||
| 110 | return $requestHander->generateErrorOutput($formType, $output); |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->dispatcher->dispatch(new PreTeamInviteEvent($user, $team, $inviteCode), PreTeamInviteEvent::NAME); |
||
| 114 | $this->inviteCodeRepository->save($inviteCode); |
||
| 115 | $message = $this->messageFactory->getTeamInviteMessage($user, $team, $inviteCode); |
||
| 116 | |||
| 117 | $this->sender->send($message); |
||
| 118 | $this->dispatcher->dispatch(new PostTeamInviteEvent($user, $team, $inviteCode), PostTeamInviteEvent::NAME); |
||
| 119 | $output['success'] = true; |
||
| 120 | $this->requestStack->getSession()->getFlashBag()->add('success', 'parthenon.user.team_invite.success'); |
||
| 121 | $this->getLogger()->info('A team invite was successfully sent'); |
||
| 122 | } catch (AlreadyInvitedException $e) { |
||
| 123 | $this->getLogger()->info('A team invite failed to be created due to one already existing'); |
||
| 124 | |||
| 125 | $this->requestStack->getSession()->getFlashBag()->add('error', 'parthenon.user.team_invite.already_invited'); |
||
| 126 | $output['already_invited'] = true; |
||
| 127 | |||
| 128 | return $requestHander->generateErrorOutput($formType, $output); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | $output['form'] = $formType->createView(); |
||
| 134 | |||
| 135 | return $requestHander->generateDefaultOutput($formType, $output); |
||
| 136 | } |
||
| 138 |