Conditions | 10 |
Paths | 20 |
Total Lines | 103 |
Code Lines | 56 |
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 |
||
61 | public function new(Request $request, EntityManagerInterface $entityManager, EventDispatcherInterface $dispatcher, |
||
62 | PaymentReferenceGenerator $paymentReferenceGenerator, RateLimiterFactory $paymentOrderSubmitLimiter): Response |
||
63 | { |
||
64 | $limiter = $paymentOrderSubmitLimiter->create($request->getClientIp()); |
||
65 | |||
66 | $new_order = new PaymentOrder(); |
||
67 | |||
68 | $blocked_token = $request->get('blocked_token'); |
||
69 | |||
70 | //Skip fsr blocked validation if a token was given (but it is not validated yet if the token is correct) |
||
71 | $validation_groups = ['Default', 'frontend']; |
||
72 | if (!$blocked_token) { |
||
73 | $validation_groups[] = 'fsr_blocked'; |
||
74 | } |
||
75 | |||
76 | $form = $this->createForm(PaymentOrderType::class, $new_order, [ |
||
77 | 'validation_groups' => $validation_groups, |
||
78 | ]); |
||
79 | |||
80 | if (!$form instanceof Form) { |
||
81 | throw new InvalidArgumentException('$form must be a Form object!'); |
||
82 | } |
||
83 | |||
84 | $form->handleRequest($request); |
||
85 | |||
86 | if ($form->isSubmitted()) { |
||
87 | if ($form->isValid()) { |
||
88 | /* Limit the amount of how many payment orders can be submitted by one user in an hour |
||
89 | This prevents automatic mass creation of payment orders and also prevents that skip token can |
||
90 | guessed by brute force */ |
||
91 | $limiter->consume(1) |
||
92 | ->ensureAccepted(); |
||
93 | |||
94 | //We know now the department and can check if token was valid |
||
95 | //If it isn't, then show an flash and dont save the payment order |
||
96 | if ($blocked_token && !$new_order->getDepartment()->isSkipBlockedValidationToken($blocked_token)) { |
||
97 | $this->addFlash('error', 'payment_order.flash.invalid_blocked_token'); |
||
98 | } else { |
||
99 | $entityManager->persist($new_order); |
||
100 | |||
101 | //Invalidate blocked token if one was given |
||
102 | if ($blocked_token) { |
||
103 | $new_order->getDepartment() |
||
104 | ->invalidateSkipBlockedValidationToken($blocked_token); |
||
105 | } |
||
106 | |||
107 | $username = sprintf('%s %s (%s) [New PaymentOrder]', |
||
108 | $new_order->getFirstName(), |
||
109 | $new_order->getLastName(), |
||
110 | $new_order->getContactEmail() |
||
111 | ); |
||
112 | $this->userProvider->setManualUsername($username, $new_order->getContactEmail()); |
||
113 | |||
114 | $entityManager->flush(); |
||
115 | |||
116 | //We have to do this after the first flush, as we need to know the ID |
||
117 | $this->userProvider->setManualUsername('[Automatic payment reference generation]', |
||
118 | UserProvider::INTERNAL_USER_IDENTIFIER); |
||
119 | $paymentReferenceGenerator->setPaymentReference($new_order); |
||
120 | $entityManager->flush(); |
||
121 | |||
122 | $this->addFlash('success', 'flash.saved_successfully'); |
||
123 | |||
124 | //Dispatch event so an email can be sent |
||
125 | $event = new PaymentOrderSubmittedEvent($new_order); |
||
126 | $dispatcher->dispatch($event, $event::NAME); |
||
127 | |||
128 | //Redirect to homepage, if no further paymentOrders should be submitted |
||
129 | //Otherwise create a new form for further ones |
||
130 | if ('submit' === $form->getClickedButton()->getName()) { |
||
131 | return $this->redirectToRoute('homepage'); |
||
132 | } |
||
133 | |||
134 | if ('submit_new' === $form->getClickedButton()->getName()) { |
||
135 | $old_order = $new_order; |
||
136 | $new_order = new PaymentOrder(); |
||
137 | $this->copyProperties($old_order, $new_order); |
||
138 | |||
139 | $form = $this->createForm(PaymentOrderType::class, $new_order); |
||
140 | } |
||
141 | } |
||
142 | } else { |
||
143 | $this->addFlash('error', 'flash.error.check_input'); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | $limit = $limiter->consume(0); |
||
148 | |||
149 | $response = $this->render('PaymentOrder/form.html.twig', [ |
||
150 | 'form' => $form->createView(), |
||
151 | 'entity' => $new_order, |
||
152 | ]); |
||
153 | |||
154 | $response->headers->add( |
||
155 | [ |
||
156 | 'X-RateLimit-Remaining' => $limit->getRemainingTokens(), |
||
157 | 'X-RateLimit-Retry-After' => $limit->getRetryAfter() |
||
158 | ->getTimestamp(), |
||
159 | 'X-RateLimit-Limit' => $limit->getLimit(), |
||
160 | ] |
||
161 | ); |
||
162 | |||
163 | return $response; |
||
164 | } |
||
289 |