Completed
Push — master ( 341936...178071 )
by Florian
03:31
created

PreCheck::sendRequest()   D

Complexity

Conditions 10
Paths 288

Size

Total Lines 61
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
rs 4.7368
cc 10
eloc 37
nc 288
nop 5

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Model\Api\Request\Genericpayment;
28
29
use Payone\Core\Model\Methods\PayoneMethod;
30
use Magento\Quote\Model\Quote;
31
32
/**
33
 * Class for the PAYONE Server API request genericpayment - "pre_check"
34
 */
35
class PreCheck extends Base
36
{
37
    /**
38
     * Send request to PAYONE Server-API with request-type "genericpayment" and action "pre_check"
39
     *
40
     * @param  PayoneMethod $oPayment payment object
41
     * @param  Quote        $oQuote   order object
42
     * @param  float        $dAmount  order sum amount
43
     * @param  string|bool  $sBirthday
44
     * @param  string|bool  $sEmail
45
     * @return array
46
     */
47
    public function sendRequest(PayoneMethod $oPayment, Quote $oQuote, $dAmount, $sBirthday = false, $sEmail = false)
48
    {
49
        $this->addParameter('request', 'genericpayment');
50
        $this->addParameter('add_paydata[action]', 'pre_check');
51
52
        $this->addParameter('mode', $oPayment->getOperationMode());
53
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); // ID of PayOne Sub-Account
54
        $this->addParameter('api_version', '3.10');
55
56
        $this->addParameter('clearingtype', $oPayment->getClearingtype());
0 ignored issues
show
Bug introduced by
It seems like $oPayment->getClearingtype() targeting Payone\Core\Model\Method...thod::getClearingtype() can also be of type boolean; however, Payone\Core\Model\Api\Request\Base::addParameter() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
57
        $this->addParameter('financingtype', $oPayment->getSubType());
0 ignored issues
show
Bug introduced by
It seems like $oPayment->getSubType() targeting Payone\Core\Model\Method...oneMethod::getSubType() can also be of type boolean; however, Payone\Core\Model\Api\Request\Base::addParameter() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58
        $this->addParameter('add_paydata[payment_type]', $oPayment->getLongSubType());
59
60
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100);
61
        $this->addParameter('currency', $oQuote->getQuoteCurrencyCode());
62
63
        if ($sEmail === false) {
64
            $sEmail = $oQuote->getCustomerEmail();
65
        }
66
        $this->addParameter('email', $sEmail);
67
68
        #if ($sBirthday === false && $oPayment->getData('info_instance')) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
        if ($oPayment->getData('info_instance')) {
70
            $sBirthday = $oPayment->getInfoInstance()->getAdditionalInformation('dateofbirth');
71
        }
72
        if ($sBirthday) {
73
            $this->addParameter('birthday', $sBirthday);
74
        }
75
76
        $oBilling = $oQuote->getBillingAddress();
77
        $this->addAddress($oBilling);
78
79
        if ($oBilling->getCountryId() == 'NL') {
80
            $sTelephone = $oBilling->getTelephone();
81
            if (empty($sTelephone)) {
82
                $sTelephone = $oPayment->getInfoInstance()->getAdditionalInformation('telephone');
83
            }
84
            $this->addParameter('telephone', $sTelephone);
85
        }
86
87
        $this->addParameter('language', $this->shopHelper->getLocale());
88
89
        $sIp = $this->environmentHelper->getRemoteIp(); // get remote IP
90
        if ($sIp != '') {// is IP not empty
91
            $this->addParameter('ip', $sIp); // add IP address to the request
92
        }
93
94
        if ($oPayment->getData('info_instance')) {
95
            $sTradeRegistryNumber = $oPayment->getInfoInstance()->getAdditionalInformation('trade_registry_number');
96
            if ($sTradeRegistryNumber) {
97
                $this->addParameter('add_paydata[b2b]', 'yes');
98
                $this->addParameter('add_paydata[company_trade_registry_number]', $sTradeRegistryNumber);
99
            }
100
        }
101
102
        if ($oPayment->hasCustomConfig()) {// if payment type doesnt use the global settings
103
            $this->addCustomParameters($oPayment); // add custom connection settings
104
        }
105
106
        return $this->send();
107
    }
108
}
109