Total Complexity | 55 |
Total Lines | 394 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like PayoneMethod often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PayoneMethod, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | abstract class PayoneMethod extends BaseMethod |
||
39 | { |
||
40 | /** |
||
41 | * Returns clearingtype |
||
42 | * |
||
43 | * @return string |
||
44 | */ |
||
45 | public function getClearingtype() |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns authorization-mode |
||
52 | * preauthorization or authorization |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getAuthorizationMode() |
||
57 | { |
||
58 | $sRequestType = $this->shopHelper->getConfigParam('request_type'); |
||
59 | if ($this->hasCustomConfig()) { |
||
60 | $sCustomRequestType = $this->getCustomConfigParam('request_type'); |
||
61 | if (!empty($sCustomRequestType)) { |
||
62 | $sRequestType = $sCustomRequestType; |
||
63 | } |
||
64 | } |
||
65 | return $sRequestType; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Method handling the debit request and the response |
||
70 | * |
||
71 | * @param InfoInterface $payment |
||
72 | * @param float $amount |
||
73 | * @return void |
||
74 | * @throws LocalizedException |
||
75 | */ |
||
76 | protected function sendPayoneDebit(InfoInterface $payment, $amount) |
||
77 | { |
||
78 | if ($this->shopHelper->getConfigParam('currency') == 'display' && $payment->getCreditmemo()) { |
||
79 | $amount = $payment->getCreditmemo()->getGrandTotal(); // send display amount instead of base amount |
||
80 | } |
||
81 | $aResponse = $this->debitRequest->sendRequest($this, $payment, $amount); |
||
82 | if (!$aResponse) { |
||
83 | throw new LocalizedException(__('Unkown error')); |
||
84 | } elseif ($aResponse['status'] == 'ERROR') { |
||
85 | $this->checkoutSession->setPayoneDebitRequest($this->debitRequest->getParameters()); |
||
86 | $this->checkoutSession->setPayoneDebitResponse($this->debitRequest->getResponse()); |
||
87 | $this->checkoutSession->setPayoneDebitOrderId($this->debitRequest->getOrderId()); |
||
88 | throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage'])); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Method handling the capture request and the response |
||
94 | * |
||
95 | * @param InfoInterface $payment |
||
96 | * @param float $amount |
||
97 | * @return void |
||
98 | * @throws LocalizedException |
||
99 | */ |
||
100 | protected function sendPayoneCapture(InfoInterface $payment, $amount) |
||
101 | { |
||
102 | if ($this->shopHelper->getConfigParam('currency') == 'display' && $payment->getOrder()->hasInvoices()) { |
||
103 | $oInvoice = $payment->getOrder()->getInvoiceCollection()->getLastItem(); |
||
104 | $amount = $oInvoice->getGrandTotal(); // send display amount instead of base amount |
||
105 | } |
||
106 | $aResponse = $this->captureRequest->sendRequest($this, $payment, $amount); |
||
107 | if (!$aResponse) {// response not existing |
||
108 | throw new LocalizedException(__('Unkown error')); |
||
109 | } elseif ($aResponse['status'] == 'ERROR') {// request returned an error |
||
110 | throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage'])); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Removes status flag used during checkout process from session |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | protected function unsetSessionStatusFlags() { |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Method handling the authorization request and the response |
||
130 | * |
||
131 | * @param InfoInterface $payment |
||
132 | * @param float $amount |
||
133 | * @return void |
||
134 | * @throws LocalizedException |
||
135 | */ |
||
136 | protected function sendPayoneAuthorization(InfoInterface $payment, $amount) |
||
137 | { |
||
138 | $this->unsetSessionStatusFlags(); |
||
139 | $oOrder = $payment->getOrder(); |
||
140 | $oOrder->setCanSendNewEmailFlag(false); // dont send email now, will be sent on appointed |
||
141 | |||
142 | if ($this->shopHelper->getConfigParam('currency') == 'display') { |
||
143 | $amount = $oOrder->getTotalDue(); // send display amount instead of base amount |
||
144 | } |
||
145 | |||
146 | $this->checkoutSession->unsPayoneRedirectUrl(); // remove redirect url from session |
||
147 | $this->checkoutSession->unsPayoneRedirectedPaymentMethod(); |
||
148 | $this->checkoutSession->unsPayoneCanceledPaymentMethod(); |
||
149 | $this->checkoutSession->unsPayoneIsError(); |
||
150 | |||
151 | $aResponse = $this->authorizationRequest->sendRequest($this, $oOrder, $amount); |
||
152 | $aResponse = $this->handleResponse($aResponse, $oOrder, $amount); |
||
153 | if ($aResponse['status'] == 'ERROR') {// request returned an error |
||
154 | throw new AuthorizationException(__($aResponse['errorcode'].' - '.$aResponse['customermessage']), $aResponse); |
||
155 | } elseif ($aResponse['status'] == 'APPROVED' || $aResponse['status'] == 'REDIRECT') {// request successful |
||
156 | $payment->setTransactionId($aResponse['txid']); |
||
157 | $payment->setIsTransactionClosed(0); |
||
158 | if ($aResponse['status'] == 'REDIRECT') {// user needs to be redirected to external payment page |
||
159 | $this->checkoutSession->setPayoneRedirectUrl($aResponse['redirecturl']); |
||
160 | $this->checkoutSession->setPayoneRedirectedPaymentMethod($this->getCode()); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Perform certain actions with the response |
||
167 | * Extension hook for certain payment methods |
||
168 | * |
||
169 | * @param array $aResponse |
||
170 | * @param Order $oOrder |
||
171 | * @param float $amount |
||
172 | * @return array |
||
173 | */ |
||
174 | protected function handleResponse($aResponse, Order $oOrder, $amount) |
||
175 | { |
||
176 | $aAddData = $oOrder->getPayment()->getAdditionalInformation(); |
||
177 | if (!empty($aAddData['iban'])) { |
||
178 | $oOrder->getPayment()->setAdditionalInformation('iban', $this->toolkitHelper->maskIban($aAddData['iban'])); |
||
179 | } |
||
180 | return $aResponse; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Convert DataObject to needed array format |
||
185 | * Hook for overriding in specific payment type class |
||
186 | * |
||
187 | * @param DataObject $data |
||
188 | * @return array |
||
189 | */ |
||
190 | protected function getPaymentStorageData(DataObject $data) |
||
191 | { |
||
192 | return []; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Check config and save payment data |
||
197 | * |
||
198 | * @param DataObject $data |
||
199 | * @return void |
||
200 | */ |
||
201 | protected function handlePaymentDataStorage(DataObject $data) |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Returns operationmode live or test for this payment method |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getOperationMode() |
||
218 | { |
||
219 | return $this->getCustomConfigParam('mode'); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Return parameters specific to this payment type |
||
224 | * |
||
225 | * @param Order $oOrder |
||
226 | * @return array |
||
227 | */ |
||
228 | public function getPaymentSpecificParameters(Order $oOrder) |
||
229 | { |
||
230 | return []; // filled in child classes |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Return success url for redirect payment types |
||
235 | * |
||
236 | * @param Order $oOrder |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getSuccessUrl(Order $oOrder = null) |
||
240 | { |
||
241 | $sAddedParams = ''; |
||
242 | if ($oOrder !== null) { |
||
243 | $sAddedParams = '?incrementId='.$oOrder->getIncrementId(); |
||
244 | } |
||
245 | return $this->url->getUrl('payone/onepage/returned').$sAddedParams; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return cancel url for redirect payment types |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getCancelUrl() |
||
254 | { |
||
255 | return $this->url->getUrl('payone/onepage/cancel'); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Return error url for redirect payment types |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getErrorUrl() |
||
264 | { |
||
265 | return $this->url->getUrl('payone/onepage/cancel?error=1'); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Return if redirect urls have to be added to the authroization request |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function needsRedirectUrls() |
||
274 | { |
||
275 | return $this->blNeedsRedirectUrls; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Return if transaction_param has to be added to the authroization request |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function needsTransactionParam() |
||
284 | { |
||
285 | return $this->blNeedsTransactionParam; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Return if invoice data has to be added to the authroization request |
||
290 | * |
||
291 | * @return bool |
||
292 | */ |
||
293 | public function needsProductInfo() |
||
294 | { |
||
295 | return $this->blNeedsProductInfo; |
||
296 | } |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Return if bank data has to be added to the debit request |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function needsSepaDataOnDebit() |
||
305 | { |
||
306 | return $this->blNeedsSepaDataOnDebit; |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Get config parameter for this payment type |
||
311 | * |
||
312 | * @param string $sParam |
||
313 | * @param string $sStoreCode |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getCustomConfigParam($sParam, $sStoreCode = null) |
||
317 | { |
||
318 | if ($sStoreCode === null) { |
||
319 | $sStoreCode = $this->getStoreCode(); |
||
320 | } |
||
321 | return $this->shopHelper->getConfigParam($sParam, $this->getCode(), 'payone_payment', $sStoreCode); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Trys to retrieve the storecode from the order |
||
326 | * |
||
327 | * @return string|null |
||
328 | */ |
||
329 | protected function getStoreCode() |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Returns if global PAYONE config is used for this payment type |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | public function hasCustomConfig() |
||
361 | { |
||
362 | if ($this->getCustomConfigParam('use_global') == '0') {// has non-global config |
||
363 | return true; |
||
364 | } |
||
365 | return false; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Return if this payment method is part of a group |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | public function isGroupMethod() |
||
374 | { |
||
375 | if ($this->sGroupName === false) { |
||
376 | return false; |
||
377 | } |
||
378 | return true; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Returns group identifier |
||
383 | * |
||
384 | * @return string|bool |
||
385 | */ |
||
386 | public function getGroupName() |
||
387 | { |
||
388 | return $this->sGroupName; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Returns group identifier |
||
393 | * |
||
394 | * @return string|bool |
||
395 | */ |
||
396 | public function getSubType() |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Return parameters specific to this payment sub type |
||
403 | * |
||
404 | * @param Order $oOrder |
||
405 | * @return array |
||
406 | */ |
||
407 | public function getSubTypeSpecificParameters(Order $oOrder) |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Formats the reference number if needed for this payment method |
||
414 | * Needed for Paydirekt |
||
415 | * |
||
416 | * @param string $sRefNr |
||
417 | * @return string |
||
418 | */ |
||
419 | public function formatReferenceNumber($sRefNr) |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Return max length of narrative text |
||
426 | * |
||
427 | * @return int |
||
428 | */ |
||
429 | public function getNarrativeTextMaxLength() |
||
432 | } |
||
433 | } |
||
434 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths