AddressRequest::addUserDataParameters()   C
last analyzed

Complexity

Conditions 12
Paths 16

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 16
nc 16
nop 6

How to fix   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;
28
29
use Payone\Core\Helper\Country as CountryHelper;
30
use Payone\Core\Model\PayoneConfig;
31
use Payone\Core\Model\Methods\PayoneMethod;
32
use Magento\Quote\Model\Quote\Address as QuoteAddress;
33
use Magento\Sales\Model\Order\Address as OrderAddress;
34
35
/**
36
 * Base class for all PAYONE requests that need the address methods
37
 */
38
abstract class AddressRequest extends Base
39
{
40
    /**
41
     * PAYONE customer helper
42
     *
43
     * @var \Payone\Core\Helper\Customer
44
     */
45
    protected $customerHelper;
46
47
    /**
48
     * Constructor
49
     *
50
     * @param \Payone\Core\Helper\Shop                $shopHelper
51
     * @param \Payone\Core\Helper\Environment         $environmentHelper
52
     * @param \Payone\Core\Helper\Api                 $apiHelper
53
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
54
     * @param \Payone\Core\Helper\Customer            $customerHelper
55
     */
56
    public function __construct(
57
        \Payone\Core\Helper\Shop $shopHelper,
58
        \Payone\Core\Helper\Environment $environmentHelper,
59
        \Payone\Core\Helper\Api $apiHelper,
60
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
61
        \Payone\Core\Helper\Customer $customerHelper
62
    ) {
63
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog);
64
        $this->customerHelper = $customerHelper;
65
    }
66
67
    /**
68
     * Add address-parameters to the request
69
     *
70
     * @param  QuoteAddress|OrderAddress $oAddress
71
     * @param  bool                      $blIsShipping
72
     * @return void
73
     */
74
    protected function addAddress($oAddress, $blIsShipping = false)
75
    {
76
        $sPre = '';
77
        if ($blIsShipping === true) {
78
            $sPre = 'shipping_'; // add shipping prefix for shipping addresses
79
        }
80
        $this->addParameter($sPre.'firstname', $oAddress->getFirstname());
81
        $this->addParameter($sPre.'lastname', $oAddress->getLastname());
82
        if ($oAddress->getCompany()) {// company name existing?
83
            $this->addParameter($sPre.'company', $oAddress->getCompany());
84
        }
85
86
        $aStreet = $oAddress->getStreet();
87
        $sStreet = is_array($aStreet) ? implode(' ', $aStreet) : $aStreet; // street may be an array
88
        $this->addParameter($sPre.'street', trim($sStreet));
89
        $this->addParameter($sPre.'zip', $oAddress->getPostcode());
90
        $this->addParameter($sPre.'city', $oAddress->getCity());
91
        $this->addParameter($sPre.'country', $oAddress->getCountryId());
92
93
        if (CountryHelper::isStateNeeded($oAddress->getCountryId()) && $oAddress->getRegionCode()) {
94
            $this->addParameter($sPre.'state', $this->customerHelper->getRegionCode($oAddress));
95
        }
96
    }
97
98
    /**
99
     * Add user-data to the request
100
     *
101
     * @param  QuoteAddress|OrderAddress $oBilling
102
     * @param  PayoneMethod              $oPayment
103
     * @param  string                    $iGender
104
     * @param  string                    $sEmail
105
     * @param  string                    $sDob
106
     * @param  bool                      $blIsUpdateUser
107
     * @return void
108
     */
109
    protected function addUserDataParameters($oBilling, PayoneMethod $oPayment, $iGender, $sEmail, $sDob, $blIsUpdateUser = false)
110
    {
111
        $this->addAddress($oBilling);
112
113
        if ($iGender || $blIsUpdateUser) {
114
            $this->addParameter('salutation', $this->customerHelper->getSalutationParameter($iGender), $blIsUpdateUser);
115
            $this->addParameter('gender', $this->customerHelper->getGenderParameter($iGender), $blIsUpdateUser);
116
        }
117
118
        $this->addParameter('email', $sEmail);
119
        if ($blIsUpdateUser || $oBilling->getTelephone()) {
120
            $this->addParameter('telephonenumber', $oBilling->getTelephone(), $blIsUpdateUser);
121
        }
122
123
        if ((
124
                in_array($oPayment->getCode(), [PayoneConfig::METHOD_KLARNA]) &&
125
                in_array($oBilling->getCountryId(), ['DE', 'NL', 'AT'])
126
            ) || ($blIsUpdateUser || ($sDob != '0000-00-00 00:00:00' && $sDob != ''))
127
        ) {
128
            $this->addParameter('birthday', str_replace('-', '', date('Ymd', strtotime($sDob)), $blIsUpdateUser));
129
        }
130
131
        $this->addParameter('language', $this->shopHelper->getLocale());
132
        if ($blIsUpdateUser || $oBilling->getVatId() != '') {
133
            $this->addParameter('vatid', $oBilling->getVatId(), $blIsUpdateUser);
0 ignored issues
show
Bug introduced by
It seems like $blIsUpdateUser can also be of type integer or null; however, Payone\Core\Model\Api\Request\Base::addParameter() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
134
        }
135
    }
136
}
137