Conditions | 22 |
Paths | 358 |
Total Lines | 110 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
178 | public function confirmation(?PaymentOrder $paymentOrder, Request $request, EntityManagerInterface $em): Response |
||
179 | { |
||
180 | if($paymentOrder === null) { |
||
181 | $this->addFlash('error', 'payment_order.can_not_be_found'); |
||
182 | return $this->redirectToRoute('homepage'); |
||
183 | } |
||
184 | |||
185 | //Check if we have one of the valid confirm numbers |
||
186 | $confirm_step = $request->query->getInt('confirm'); |
||
187 | if (1 !== $confirm_step && 2 !== $confirm_step) { |
||
188 | $this->addFlash('error', 'payment_order.confirmation.invalid_step'); |
||
189 | |||
190 | return $this->redirectToRoute('homepage'); |
||
191 | } |
||
192 | |||
193 | //Check if given token is correct for this step |
||
194 | $correct_token = (1 === $confirm_step) ? $paymentOrder->getConfirm1Token() : $paymentOrder->getConfirm2Token(); |
||
195 | if (null === $correct_token) { |
||
196 | throw new RuntimeException('This payment_order can not be confirmed! No token is set.'); |
||
197 | } |
||
198 | |||
199 | $given_token = (string) $request->query->get('token'); |
||
200 | if (!password_verify($given_token, $correct_token)) { |
||
201 | $this->addFlash('error', 'payment_order.confirmation.invalid_token'); |
||
202 | |||
203 | return $this->redirectToRoute('homepage'); |
||
204 | } |
||
205 | |||
206 | $already_confirmed = false; |
||
207 | |||
208 | //Check if it was already confirmed from this side and disable form if needed |
||
209 | $confirm_timestamp = (1 === $confirm_step) ? $paymentOrder->getConfirm1Timestamp() : $paymentOrder->getConfirm2Timestamp(); |
||
210 | if (null !== $confirm_timestamp) { |
||
211 | $already_confirmed = true; |
||
212 | } |
||
213 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
214 | 'disabled' => null !== $confirm_timestamp, |
||
215 | ]); |
||
216 | |||
217 | $paymentOrder_is_undeletable = $paymentOrder->isExported() |
||
218 | || $paymentOrder->isMathematicallyCorrect() |
||
219 | || $paymentOrder->isFactuallyCorrect() |
||
220 | || null != $paymentOrder->getBookingDate(); |
||
221 | |||
222 | $deletion_form = $this->createFormBuilder() |
||
223 | ->add('delete', SubmitType::class, [ |
||
224 | 'disabled' => $paymentOrder_is_undeletable, |
||
225 | 'label' => 'payment_order.confirm.delete.btn', |
||
226 | 'attr' => [ |
||
227 | 'class' => 'btn btn-danger' |
||
228 | ] |
||
229 | ])->getForm(); |
||
230 | |||
231 | //Handle deletion form |
||
232 | $deletion_form->handleRequest($request); |
||
233 | if ($deletion_form->isSubmitted() && $deletion_form->isValid()) { |
||
234 | if ($paymentOrder_is_undeletable) { |
||
235 | throw new RuntimeException("This payment order is already exported or booked and therefore can not be deleted by user!"); |
||
236 | } |
||
237 | |||
238 | $blame_user = "unknown"; |
||
239 | if ($confirm_step === 1) { |
||
240 | $blame_user = implode(",", $paymentOrder->getDepartment()->getEmailHhv()); |
||
241 | } elseif ($confirm_step === 2) { |
||
242 | $blame_user = implode(',', $paymentOrder->getDepartment()->getEmailTreasurer()); |
||
243 | } |
||
244 | |||
245 | $message = new PaymentOrderDeletedNotification($paymentOrder, $blame_user, PaymentOrderDeletedNotification::DELETED_WHERE_FRONTEND); |
||
246 | $this->dispatchMessage($message); |
||
|
|||
247 | |||
248 | $this->entityManager->remove($paymentOrder); |
||
249 | $this->entityManager->flush(); |
||
250 | |||
251 | $this->addFlash('success', 'payment_order.confirmation.delete.success'); |
||
252 | return $this->redirectToRoute('homepage'); |
||
253 | } |
||
254 | |||
255 | //Handle confirmation form |
||
256 | $form->handleRequest($request); |
||
257 | if ($form->isSubmitted() && $form->isValid()) { |
||
258 | $this->addFlash('success', 'payment_order.confirmation.success'); |
||
259 | //Write confirmation to DB |
||
260 | if (1 === $confirm_step) { |
||
261 | $paymentOrder->setConfirm1Timestamp(new DateTime()); |
||
262 | } elseif (2 === $confirm_step) { |
||
263 | $paymentOrder->setConfirm2Timestamp(new DateTime()); |
||
264 | } |
||
265 | |||
266 | //Add hintful information about who did this, to audit log |
||
267 | $emails = (1 === $confirm_step) ? $paymentOrder->getDepartment() |
||
268 | ->getEmailHhv() : $paymentOrder->getDepartment() |
||
269 | ->getEmailTreasurer(); |
||
270 | $username = sprintf('%s [Confirmation %d]', implode(', ', $emails), $confirm_step); |
||
271 | $this->userProvider->setManualUsername($username, implode(',', $emails)); |
||
272 | $em->flush(); |
||
273 | |||
274 | //Rerender form if it was confirmed, to apply the disabled state |
||
275 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
276 | 'disabled' => true, |
||
277 | ]); |
||
278 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
279 | } |
||
280 | |||
281 | return $this->render('PaymentOrder/confirm/confirm.html.twig', [ |
||
282 | 'entity' => $paymentOrder, |
||
283 | 'confirmation_nr' => $confirm_step, |
||
284 | 'form' => $form->createView(), |
||
285 | 'deletion_form' => $deletion_form->createView(), |
||
286 | 'paymentOrder_is_undeletable' => $paymentOrder_is_undeletable, |
||
287 | 'already_confirmed' => $already_confirmed, |
||
288 | ]); |
||
291 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.