Conditions | 12 |
Paths | 37 |
Total Lines | 55 |
Code Lines | 31 |
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 |
||
110 | public function confirmation(PaymentOrder $paymentOrder, Request $request, EntityManagerInterface $em): Response |
||
111 | { |
||
112 | //Check if we have one of the valid confirm numbers |
||
113 | $confirm_step = $request->query->getInt('confirm'); |
||
114 | if (1 !== $confirm_step && 2 !== $confirm_step) { |
||
115 | //$this->createNotFoundException('Invalid confirmation step! Only 1 or 2 are allowed.'); |
||
116 | $this->addFlash('error', 'payment_order.confirmation.invalid_step'); |
||
117 | |||
118 | return $this->redirectToRoute('homepage'); |
||
119 | } |
||
120 | |||
121 | //Check if given token is correct for this step |
||
122 | $correct_token = (1 === $confirm_step) ? $paymentOrder->getConfirm1Token() : $paymentOrder->getConfirm2Token(); |
||
123 | if (null === $correct_token) { |
||
124 | throw new RuntimeException('This payment_order can not be confirmed! No token is set.'); |
||
125 | } |
||
126 | |||
127 | $given_token = (string) $request->query->get('token'); |
||
128 | if (!password_verify($given_token, $correct_token)) { |
||
129 | $this->addFlash('error', 'payment_order.confirmation.invalid_token'); |
||
130 | |||
131 | return $this->redirectToRoute('homepage'); |
||
132 | } |
||
133 | |||
134 | //Check if it was already confirmed from this side and disable form if needed |
||
135 | $confirm_timestamp = (1 === $confirm_step) ? $paymentOrder->getConfirm1Timestamp() : $paymentOrder->getConfirm2Timestamp(); |
||
136 | if (null !== $confirm_timestamp) { |
||
137 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
138 | } |
||
139 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
140 | 'disabled' => null !== $confirm_timestamp, |
||
141 | ]); |
||
142 | |||
143 | $form->handleRequest($request); |
||
144 | if ($form->isSubmitted() && $form->isValid()) { |
||
145 | $this->addFlash('success', 'payment_order.confirmation.success'); |
||
146 | //Write confirmation to DB |
||
147 | if (1 === $confirm_step) { |
||
148 | $paymentOrder->setConfirm1Timestamp(new DateTime()); |
||
149 | } elseif (2 === $confirm_step) { |
||
150 | $paymentOrder->setConfirm2Timestamp(new DateTime()); |
||
151 | } |
||
152 | $em->flush(); |
||
153 | |||
154 | //Rerender form if it was confirmed, to apply the disabled state |
||
155 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
156 | 'disabled' => true, |
||
157 | ]); |
||
158 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
159 | } |
||
160 | |||
161 | return $this->render('PaymentOrder/confirm/confirm.html.twig', [ |
||
162 | 'entity' => $paymentOrder, |
||
163 | 'confirmation_nr' => $confirm_step, |
||
164 | 'form' => $form->createView(), |
||
165 | ]); |
||
168 |