Conditions | 12 |
Paths | 300 |
Total Lines | 130 |
Code Lines | 90 |
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 |
||
77 | public function process( |
||
78 | IAuthenticationProcess $process, |
||
79 | ?ServerRequestInterface $httpRequest |
||
80 | ): IChallengeResponse { |
||
81 | $username = $process |
||
|
|||
82 | ->getTypedMap() |
||
83 | ->get('username', StringObject::class) |
||
84 | ->toString() |
||
85 | ; |
||
86 | |||
87 | $usedU2fKeys = $process |
||
88 | ->getTypedMap() |
||
89 | ->get('used_u2f_key_public_keys', ArrayObject::class) |
||
90 | ->toArray(Scalar::_STR) |
||
91 | ; |
||
92 | |||
93 | $registrations = $this |
||
94 | ->appConfig |
||
95 | ->getU2fRegistrations($username) |
||
96 | ; |
||
97 | |||
98 | foreach ($registrations as $key => $registration) { |
||
99 | if (in_array($registration->getPublicKey(), $usedU2fKeys, true)) { |
||
100 | unset($registrations[$key]); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | $form = $this |
||
105 | ->formFactory |
||
106 | ->createBuilder() |
||
107 | ->add('u2fTokenResponse', HiddenType::class) |
||
108 | ->getForm() |
||
109 | ; |
||
110 | |||
111 | if (null !== $httpRequest) { |
||
112 | $form->handleRequest($this->httpFoundationFactory->createRequest($httpRequest)); |
||
113 | } |
||
114 | try { |
||
115 | if ($form->isSubmitted() && $form->isValid()) { |
||
116 | $signRequests = $process |
||
117 | ->getTypedMap() |
||
118 | ->get('u2f_sign_requests', ArrayObject::class) |
||
119 | ; |
||
120 | $newRegistration = $this |
||
121 | ->u2fAuthenticationManager |
||
122 | ->processResponse( |
||
123 | new ArrayObject($registrations, IU2fRegistration::class), |
||
124 | $signRequests, |
||
125 | $form['u2fTokenResponse']->getData() |
||
126 | ) |
||
127 | ; |
||
128 | foreach ($registrations as $key => $registration) { |
||
129 | if ($registration->getPublicKey() === $newRegistration->getPublicKey()) { |
||
130 | $registrations[$key] = $newRegistration; |
||
131 | break; |
||
132 | } |
||
133 | } |
||
134 | $newDm = $process |
||
135 | ->getTypedMap() |
||
136 | ->set( |
||
137 | 'u2f_registrations', |
||
138 | $registrations, |
||
139 | Scalar::_ARRAY |
||
140 | ) |
||
141 | ->set( |
||
142 | 'used_u2f_key_public_keys', |
||
143 | (new ArrayObject($usedU2fKeys, Scalar::_STR))->add($newRegistration->getPublicKey(), Scalar::_STR), |
||
144 | ArrayObject::class |
||
145 | ) |
||
146 | ->set( |
||
147 | 'persist_operations', |
||
148 | $process |
||
149 | ->getTypedMap() |
||
150 | ->get('persist_operations', ArrayObject::class) |
||
151 | ->add( |
||
152 | new PersistOperation($newRegistration, new Operation(Operation::UPDATE)), |
||
153 | PersistOperation::class |
||
154 | ), |
||
155 | ArrayObject::class |
||
156 | ) |
||
157 | ; |
||
158 | |||
159 | return new ChallengeResponse( |
||
160 | new AuthenticationProcess($newDm), |
||
161 | null, |
||
162 | false, |
||
163 | true |
||
164 | ) |
||
165 | ; |
||
166 | } |
||
167 | } catch (ClientErrorException $e) { |
||
168 | $form->addError(new FormError('You took too long to activate your U2F device, or the U2F device you plugged in is invalid. Please try again.')); |
||
169 | } catch (SecurityException $e) { |
||
170 | $form->addError(new FormError('The U2F key is not recognised.')); |
||
171 | } catch (NoRegisteredU2fTokenException $e) { |
||
172 | return new ChallengeResponse( |
||
173 | new AuthenticationProcess($process), |
||
174 | $httpResponse, |
||
175 | true, |
||
176 | false |
||
177 | ) |
||
178 | ; |
||
179 | } catch (UnexpectedValueException|InvalidDataException $e) { |
||
180 | $form->addError(new FormError('An error happened. Please try again.')); |
||
181 | } |
||
182 | |||
183 | $signRequests = $this |
||
184 | ->u2fAuthenticationManager |
||
185 | ->generate($username, new ArrayObject($registrations, IU2fRegistration::class)) |
||
186 | ; |
||
187 | |||
188 | $httpResponse = new Response($this->twig->render("u2f_authentication.html.twig", [ |
||
189 | "form" => $form->createView(), |
||
190 | "sign_requests_json" => json_encode(array_values($signRequests)), |
||
191 | 'nUsedU2fKeys' => count($usedU2fKeys), |
||
192 | ])); |
||
193 | $newDm = $process |
||
194 | ->getTypedMap() |
||
195 | ->set( |
||
196 | 'u2f_sign_requests', |
||
197 | new ArrayObject($signRequests, SignRequest::class), |
||
198 | ArrayObject::class |
||
199 | ) |
||
200 | ; |
||
201 | |||
202 | return new ChallengeResponse( |
||
203 | new AuthenticationProcess($newDm), |
||
204 | $httpResponse, |
||
205 | $form->isSubmitted(), |
||
206 | false |
||
207 | ) |
||
211 |