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