Conditions | 15 |
Paths | 270 |
Total Lines | 111 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
167 | public function start( Payment $payment ) { |
||
168 | $payment_method = $payment->get_method(); |
||
169 | |||
170 | $transaction_description = $payment->get_description(); |
||
171 | |||
172 | if ( empty( $transaction_description ) ) { |
||
173 | $transaction_description = $payment->get_id(); |
||
174 | } |
||
175 | |||
176 | // Merchant. |
||
177 | $merchant = new Merchant(); |
||
178 | $merchant->account = $this->config->account_id; |
||
179 | $merchant->site_id = $this->config->site_id; |
||
180 | $merchant->site_secure_code = $this->config->site_code; |
||
181 | $merchant->notification_url = $payment->get_return_url(); |
||
182 | $merchant->redirect_url = $payment->get_return_url(); |
||
183 | $merchant->cancel_url = $payment->get_return_url(); |
||
184 | $merchant->close_window = 'false'; |
||
185 | |||
186 | // Customer. |
||
187 | $customer = new Customer(); |
||
188 | $customer->ip_address = Server::get( 'REMOTE_ADDR', FILTER_VALIDATE_IP ); |
||
189 | $customer->forwarded_ip = Server::get( 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP ); |
||
190 | |||
191 | if ( null !== $payment->get_customer() ) { |
||
192 | $name = $payment->get_customer()->get_name(); |
||
193 | |||
194 | if ( null !== $name ) { |
||
195 | $customer->first_name = $name->get_first_name(); |
||
196 | $customer->last_name = $name->get_last_name(); |
||
197 | } |
||
198 | |||
199 | $customer->locale = $payment->get_customer()->get_locale(); |
||
200 | $customer->email = $payment->get_customer()->get_email(); |
||
201 | } |
||
202 | |||
203 | // Transaction. |
||
204 | $transaction = new Transaction(); |
||
205 | $transaction->id = uniqid(); |
||
206 | $transaction->currency = $payment->get_total_amount()->get_currency()->get_alphabetic_code(); |
||
207 | $transaction->amount = $payment->get_total_amount()->get_cents(); |
||
208 | $transaction->description = $transaction_description; |
||
209 | |||
210 | switch ( $payment_method ) { |
||
211 | case PaymentMethods::IDEAL: |
||
212 | $transaction->gateway = Methods::IDEAL; |
||
213 | |||
214 | $issuer = $payment->get_issuer(); |
||
215 | |||
216 | if ( empty( $issuer ) ) { |
||
217 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
218 | } else { |
||
219 | $gateway_info = new GatewayInfo(); |
||
220 | |||
221 | $gateway_info->issuer_id = $issuer; |
||
222 | |||
223 | $message = new DirectTransactionRequestMessage( $merchant, $customer, $transaction, $gateway_info ); |
||
224 | } |
||
225 | |||
226 | break; |
||
227 | case PaymentMethods::CREDIT_CARD: |
||
228 | $gateway = Methods::transform( $payment_method ); |
||
229 | |||
230 | $issuer = $payment->get_issuer(); |
||
231 | |||
232 | if ( empty( $issuer ) ) { |
||
233 | if ( $gateway ) { |
||
234 | $transaction->gateway = $gateway; |
||
235 | } |
||
236 | } else { |
||
237 | $transaction->gateway = $issuer; |
||
238 | } |
||
239 | |||
240 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
241 | |||
242 | break; |
||
243 | default: |
||
244 | $gateway = Methods::transform( $payment_method ); |
||
245 | |||
246 | if ( $gateway ) { |
||
247 | $transaction->gateway = $gateway; |
||
248 | } |
||
249 | |||
250 | if ( ! isset( $transaction->gateway ) && ! empty( $payment_method ) ) { |
||
251 | // Leap of faith if the WordPress payment method could not transform to a Mollie method? |
||
252 | $transaction->gateway = $payment_method; |
||
253 | } |
||
254 | |||
255 | $message = new RedirectTransactionRequestMessage( $merchant, $customer, $transaction ); |
||
256 | } |
||
257 | |||
258 | $signature = Signature::generate( $transaction->amount, $transaction->currency, $merchant->account, $merchant->site_id, $transaction->id ); |
||
259 | |||
260 | $message->signature = $signature; |
||
261 | |||
262 | $response = $this->client->start_transaction( $message ); |
||
263 | |||
264 | if ( $response ) { |
||
265 | $transaction = $response->transaction; |
||
266 | |||
267 | $payment->set_transaction_id( $transaction->id ); |
||
268 | |||
269 | if ( isset( $transaction->payment_url ) ) { |
||
270 | $payment->set_action_url( $transaction->payment_url ); |
||
271 | } |
||
272 | |||
273 | if ( isset( $response->gateway_info->redirect_url ) ) { |
||
274 | $payment->set_action_url( $response->gateway_info->redirect_url ); |
||
275 | } |
||
276 | } else { |
||
277 | $this->error = $this->client->get_error(); |
||
278 | } |
||
310 |