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