| Conditions | 6 |
| Paths | 5 |
| Total Lines | 54 |
| Code Lines | 35 |
| 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 |
||
| 101 | #[Template('second_factor/revoke.html.twig')] |
||
| 102 | #[Route( |
||
| 103 | path: '/second-factor/{state}/{secondFactorId}/revoke', |
||
| 104 | name: 'ss_second_factor_revoke', |
||
| 105 | requirements: ['state' => '^(unverified|verified|vetted)$'], |
||
| 106 | methods: ['GET','POST'] |
||
| 107 | )] |
||
| 108 | public function revoke(Request $request, string $state, string $secondFactorId): array|Response |
||
| 109 | { |
||
| 110 | $identity = $this->getIdentity(); |
||
| 111 | |||
| 112 | if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) { |
||
| 113 | $this->logger->error(sprintf( |
||
| 114 | 'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor', |
||
| 115 | $identity->id, |
||
| 116 | $state, |
||
| 117 | $secondFactorId |
||
| 118 | )); |
||
| 119 | throw new NotFoundHttpException(); |
||
| 120 | } |
||
| 121 | |||
| 122 | $secondFactor = match ($state) { |
||
| 123 | 'unverified' => $this->secondFactorService->findOneUnverified($secondFactorId), |
||
| 124 | 'verified' => $this->secondFactorService->findOneVerified($secondFactorId), |
||
| 125 | 'vetted' => $this->secondFactorService->findOneVetted($secondFactorId), |
||
| 126 | default => throw new LogicException('There are no other types of second factor.'), |
||
| 127 | }; |
||
| 128 | |||
| 129 | if ($secondFactor === null) { |
||
| 130 | throw new NotFoundHttpException( |
||
| 131 | sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $command = new RevokeCommand(); |
||
| 136 | $command->identity = $identity; |
||
| 137 | $command->secondFactor = $secondFactor; |
||
| 138 | |||
| 139 | $form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request); |
||
| 140 | |||
| 141 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 142 | |||
| 143 | if ($this->secondFactorService->revoke($command)) { |
||
| 144 | $this->addFlash('success', 'ss.second_factor.revoke.alert.revocation_successful'); |
||
| 145 | } else { |
||
| 146 | $this->addFlash('error', 'ss.second_factor.revoke.alert.revocation_failed'); |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->redirectToRoute('ss_second_factor_list'); |
||
| 150 | } |
||
| 151 | |||
| 152 | return [ |
||
| 153 | 'form' => $form->createView(), |
||
| 154 | 'secondFactor' => $secondFactor, |
||
| 155 | ]; |
||
| 158 |