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