| Conditions | 6 |
| Paths | 28 |
| Total Lines | 118 |
| Code Lines | 84 |
| 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 |
||
| 69 | public function process( |
||
| 70 | IAuthenticationProcess $process, |
||
| 71 | ?ServerRequestInterface $httpRequest |
||
| 72 | ): IChallengeResponse { |
||
| 73 | $u2fRegistrations = $process |
||
| 74 | ->getTypedMap() |
||
| 75 | ->get('u2f_registrations', Scalar::_ARRAY) |
||
| 76 | ; |
||
| 77 | $nU2fRegistrations = $process |
||
| 78 | ->getTypedMap() |
||
| 79 | ->get('n_u2f_registrations', IntegerObject::class) |
||
| 80 | ->toInteger() |
||
| 81 | ; |
||
| 82 | |||
| 83 | $form = $this |
||
| 84 | ->formFactory |
||
| 85 | ->createBuilder() |
||
| 86 | ->add('u2fRegistrationName') |
||
| 87 | ->add('u2fTokenResponse', HiddenType::class) |
||
| 88 | ->getForm() |
||
| 89 | ; |
||
| 90 | |||
| 91 | if (null !== $httpRequest) { |
||
| 92 | $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
||
| 93 | } |
||
| 94 | |||
| 95 | $typedMap = null; |
||
| 96 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 97 | try { |
||
| 98 | $currentU2fRegistrationRequest = $process |
||
| 99 | ->getTypedMap() |
||
| 100 | ->get('current_u2f_registration_request', U2fRegistrationRequest::class) |
||
| 101 | ; |
||
| 102 | $u2fRegTmp = $this |
||
| 103 | ->u2fRegistrationManager |
||
| 104 | ->getU2fRegistrationFromResponse( |
||
| 105 | $form['u2fTokenResponse']->getData(), |
||
| 106 | $currentU2fRegistrationRequest->getRequest() |
||
| 107 | ) |
||
| 108 | ; |
||
| 109 | $u2fRegistration = new NamedU2fRegistration( |
||
| 110 | $u2fRegTmp->getAttestationCertificate(), |
||
| 111 | $u2fRegTmp->getCounter(), |
||
| 112 | $u2fRegTmp->getKeyHandle(), |
||
| 113 | $form['u2fRegistrationName']->getData(), |
||
| 114 | $u2fRegTmp->getPublicKey() |
||
| 115 | ); |
||
| 116 | |||
| 117 | $u2fRegistrations[] = $u2fRegistration; |
||
| 118 | |||
| 119 | $typedMap = $process |
||
| 120 | ->getTypedMap() |
||
| 121 | ->set( |
||
| 122 | 'persist_operations', |
||
| 123 | $process |
||
| 124 | ->getTypedMap() |
||
| 125 | ->get('persist_operations', ArrayObject::class) |
||
| 126 | ->add( |
||
| 127 | new PersistOperation($u2fRegistration, new Operation(Operation::CREATE)), |
||
| 128 | PersistOperation::class |
||
| 129 | ), |
||
| 130 | ArrayObject::class |
||
| 131 | ) |
||
| 132 | ->set( |
||
| 133 | 'n_u2f_registrations', |
||
| 134 | new IntegerObject($nU2fRegistrations + 1), |
||
| 135 | IntegerObject::class |
||
| 136 | ) |
||
| 137 | ->set( |
||
| 138 | 'u2f_registrations', |
||
| 139 | $u2fRegistrations, |
||
| 140 | Scalar::_ARRAY |
||
| 141 | ) |
||
| 142 | ; |
||
| 143 | return new ChallengeResponse( |
||
| 144 | new AuthenticationProcess($typedMap), |
||
| 145 | null, |
||
| 146 | false, |
||
| 147 | true |
||
| 148 | ); |
||
| 149 | } catch (ClientErrorException $e) { |
||
| 150 | $form->addError(new FormError('You already used this U2F device')); |
||
| 151 | } catch (InvalidDataException $e) { |
||
| 152 | $form->addError(new FormError('The response is invalid.')); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $u2fRegistrationRequest = $this |
||
| 157 | ->u2fRegistrationManager |
||
| 158 | ->generate(new ArrayObject( |
||
| 159 | $u2fRegistrations, |
||
| 160 | IU2fRegistration::class |
||
| 161 | )) |
||
| 162 | ; |
||
| 163 | |||
| 164 | $httpResponse = new Response($this |
||
| 165 | ->twig |
||
| 166 | ->render('u2f_registration.html.twig', [ |
||
| 167 | 'form' => $form->createView(), |
||
| 168 | 'nU2fRegistrations' => $nU2fRegistrations, |
||
| 169 | 'request_json' => $u2fRegistrationRequest->getRequestAsJson(), |
||
| 170 | 'sign_requests' => $u2fRegistrationRequest->getSignRequestsAsJson(), |
||
| 171 | ])) |
||
| 172 | ; |
||
| 173 | |||
| 174 | return new ChallengeResponse( |
||
| 175 | new AuthenticationProcess( |
||
| 176 | $process |
||
| 177 | ->getTypedMap() |
||
| 178 | ->add( |
||
| 179 | 'current_u2f_registration_request', |
||
| 180 | $u2fRegistrationRequest, |
||
| 181 | U2fRegistrationRequest::class |
||
| 182 | ) |
||
| 183 | ), |
||
| 184 | $httpResponse, |
||
| 185 | false, |
||
| 186 | false |
||
| 187 | ) |
||
| 191 |