| Conditions | 21 |
| Paths | 270 |
| Total Lines | 108 |
| Code Lines | 67 |
| 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 |
||
| 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->isConfirmed() |
||
| 219 | || null != $paymentOrder->getBookingDate(); |
||
| 220 | |||
| 221 | $deletion_form = $this->createFormBuilder() |
||
| 222 | ->add('delete', SubmitType::class, [ |
||
| 223 | 'disabled' => $paymentOrder_is_undeletable, |
||
| 224 | 'label' => 'payment_order.confirm.delete.btn', |
||
| 225 | 'attr' => [ |
||
| 226 | 'class' => 'btn btn-danger' |
||
| 227 | ] |
||
| 228 | ])->getForm(); |
||
| 229 | |||
| 230 | //Handle deletion form |
||
| 231 | $deletion_form->handleRequest($request); |
||
| 232 | if ($deletion_form->isSubmitted() && $deletion_form->isValid()) { |
||
| 233 | if ($paymentOrder_is_undeletable) { |
||
| 234 | throw new RuntimeException("This payment order is already exported or booked and therefore can not be deleted by user!"); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($confirm_step === 1) { |
||
| 238 | $blame_user = implode(",", $paymentOrder->getDepartment()->getEmailHhv()); |
||
| 239 | } elseif ($confirm_step === 2) { |
||
| 240 | $blame_user = implode(',', $paymentOrder->getDepartment()->getEmailTreasurer()); |
||
| 241 | } |
||
| 242 | |||
| 243 | $message = new PaymentOrderDeletedNotification($paymentOrder, $blame_user, PaymentOrderDeletedNotification::DELETED_WHERE_FRONTEND); |
||
|
|
|||
| 244 | $this->dispatchMessage($message); |
||
| 245 | |||
| 246 | $this->entityManager->remove($paymentOrder); |
||
| 247 | $this->entityManager->flush(); |
||
| 248 | |||
| 249 | $this->addFlash('success', 'payment_order.confirmation.delete.success'); |
||
| 250 | return $this->redirectToRoute('homepage'); |
||
| 251 | } |
||
| 252 | |||
| 253 | //Handle confirmation form |
||
| 254 | $form->handleRequest($request); |
||
| 255 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 256 | $this->addFlash('success', 'payment_order.confirmation.success'); |
||
| 257 | //Write confirmation to DB |
||
| 258 | if (1 === $confirm_step) { |
||
| 259 | $paymentOrder->setConfirm1Timestamp(new DateTime()); |
||
| 260 | } elseif (2 === $confirm_step) { |
||
| 261 | $paymentOrder->setConfirm2Timestamp(new DateTime()); |
||
| 262 | } |
||
| 263 | |||
| 264 | //Add hintful information about who did this, to audit log |
||
| 265 | $emails = (1 === $confirm_step) ? $paymentOrder->getDepartment() |
||
| 266 | ->getEmailHhv() : $paymentOrder->getDepartment() |
||
| 267 | ->getEmailTreasurer(); |
||
| 268 | $username = sprintf('%s [Confirmation %d]', implode(', ', $emails), $confirm_step); |
||
| 269 | $this->userProvider->setManualUsername($username, implode(',', $emails)); |
||
| 270 | $em->flush(); |
||
| 271 | |||
| 272 | //Rerender form if it was confirmed, to apply the disabled state |
||
| 273 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
| 274 | 'disabled' => true, |
||
| 275 | ]); |
||
| 276 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $this->render('PaymentOrder/confirm/confirm.html.twig', [ |
||
| 280 | 'entity' => $paymentOrder, |
||
| 281 | 'confirmation_nr' => $confirm_step, |
||
| 282 | 'form' => $form->createView(), |
||
| 283 | 'deletion_form' => $deletion_form->createView(), |
||
| 284 | 'paymentOrder_is_undeletable' => $paymentOrder_is_undeletable, |
||
| 285 | 'already_confirmed' => $already_confirmed, |
||
| 286 | ]); |
||
| 289 |