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