Conditions | 13 |
Paths | 61 |
Total Lines | 60 |
Code Lines | 34 |
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 |
||
129 | public function confirmation(PaymentOrder $paymentOrder, Request $request, EntityManagerInterface $em): Response |
||
130 | { |
||
131 | //Check if we have one of the valid confirm numbers |
||
132 | $confirm_step = $request->query->getInt('confirm'); |
||
133 | if (1 !== $confirm_step && 2 !== $confirm_step) { |
||
134 | //$this->createNotFoundException('Invalid confirmation step! Only 1 or 2 are allowed.'); |
||
135 | $this->addFlash('error', 'payment_order.confirmation.invalid_step'); |
||
136 | |||
137 | return $this->redirectToRoute('homepage'); |
||
138 | } |
||
139 | |||
140 | //Check if given token is correct for this step |
||
141 | $correct_token = (1 === $confirm_step) ? $paymentOrder->getConfirm1Token() : $paymentOrder->getConfirm2Token(); |
||
142 | if (null === $correct_token) { |
||
143 | throw new RuntimeException('This payment_order can not be confirmed! No token is set.'); |
||
144 | } |
||
145 | |||
146 | $given_token = (string) $request->query->get('token'); |
||
147 | if (!password_verify($given_token, $correct_token)) { |
||
148 | $this->addFlash('error', 'payment_order.confirmation.invalid_token'); |
||
149 | |||
150 | return $this->redirectToRoute('homepage'); |
||
151 | } |
||
152 | |||
153 | //Check if it was already confirmed from this side and disable form if needed |
||
154 | $confirm_timestamp = (1 === $confirm_step) ? $paymentOrder->getConfirm1Timestamp() : $paymentOrder->getConfirm2Timestamp(); |
||
155 | if (null !== $confirm_timestamp) { |
||
156 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
157 | } |
||
158 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
159 | 'disabled' => null !== $confirm_timestamp, |
||
160 | ]); |
||
161 | |||
162 | $form->handleRequest($request); |
||
163 | if ($form->isSubmitted() && $form->isValid()) { |
||
164 | $this->addFlash('success', 'payment_order.confirmation.success'); |
||
165 | //Write confirmation to DB |
||
166 | if (1 === $confirm_step) { |
||
167 | $paymentOrder->setConfirm1Timestamp(new DateTime()); |
||
168 | } elseif (2 === $confirm_step) { |
||
169 | $paymentOrder->setConfirm2Timestamp(new DateTime()); |
||
170 | } |
||
171 | |||
172 | //Add hintful information about who did this, to audit log |
||
173 | $emails = (1 === $confirm_step) ? $paymentOrder->getDepartment()->getEmailHhv() : $paymentOrder->getDepartment()->getEmailTreasurer(); |
||
174 | $username = sprintf('%s [Confirmation %d]', implode(', ', $emails), $confirm_step); |
||
175 | $this->userProvider->setManualUsername($username, implode(',', $emails)); |
||
176 | $em->flush(); |
||
177 | |||
178 | //Rerender form if it was confirmed, to apply the disabled state |
||
179 | $form = $this->createForm(PaymentOrderConfirmationType::class, null, [ |
||
180 | 'disabled' => true, |
||
181 | ]); |
||
182 | $this->addFlash('info', 'payment_order.confirmation.already_confirmed'); |
||
183 | } |
||
184 | |||
185 | return $this->render('PaymentOrder/confirm/confirm.html.twig', [ |
||
186 | 'entity' => $paymentOrder, |
||
187 | 'confirmation_nr' => $confirm_step, |
||
188 | 'form' => $form->createView(), |
||
189 | ]); |
||
192 |