| Conditions | 6 |
| Paths | 5 |
| Total Lines | 54 |
| Code Lines | 35 |
| 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 |
||
| 44 | #[Template('second_factor/revoke.html.twig')] |
||
| 45 | #[Route( |
||
| 46 | path: '/second-factor/{state}/{secondFactorId}/revoke', |
||
| 47 | name: 'ss_second_factor_revoke', |
||
| 48 | requirements: ['state' => '^(unverified|verified|vetted)$'], |
||
| 49 | methods: ['GET','POST'] |
||
| 50 | )] |
||
| 51 | public function __invoke(Request $request, string $state, string $secondFactorId): array|Response |
||
| 52 | { |
||
| 53 | $identity = $this->getUser()->getIdentity(); |
||
| 54 | |||
| 55 | if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) { |
||
| 56 | $this->logger->error(sprintf( |
||
| 57 | 'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor', |
||
| 58 | $identity->id, |
||
| 59 | $state, |
||
| 60 | $secondFactorId |
||
| 61 | )); |
||
| 62 | throw new NotFoundHttpException(); |
||
| 63 | } |
||
| 64 | |||
| 65 | $secondFactor = match ($state) { |
||
| 66 | 'unverified' => $this->secondFactorService->findOneUnverified($secondFactorId), |
||
| 67 | 'verified' => $this->secondFactorService->findOneVerified($secondFactorId), |
||
| 68 | 'vetted' => $this->secondFactorService->findOneVetted($secondFactorId), |
||
| 69 | default => throw new LogicException('There are no other types of second factor.'), |
||
| 70 | }; |
||
| 71 | |||
| 72 | if ($secondFactor === null) { |
||
| 73 | throw new NotFoundHttpException( |
||
| 74 | sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId) |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | $command = new RevokeCommand(); |
||
| 79 | $command->identity = $identity; |
||
| 80 | $command->secondFactor = $secondFactor; |
||
| 81 | |||
| 82 | $form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request); |
||
| 83 | |||
| 84 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 85 | |||
| 86 | if ($this->secondFactorService->revoke($command)) { |
||
| 87 | $this->addFlash('success', 'ss.second_factor.revoke.alert.revocation_successful'); |
||
| 88 | } else { |
||
| 89 | $this->addFlash('error', 'ss.second_factor.revoke.alert.revocation_failed'); |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->redirectToRoute('ss_second_factor_list'); |
||
| 93 | } |
||
| 94 | |||
| 95 | return [ |
||
| 96 | 'form' => $form->createView(), |
||
| 97 | 'secondFactor' => $secondFactor, |
||
| 98 | ]; |
||
| 101 |