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