Passed
Pull Request — master (#301)
by
unknown
02:47
created

AddressRequest::addUserDataParameters()   B

Complexity

Conditions 10
Paths 16

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 12
c 1
b 0
f 0
nc 16
nop 6
dl 0
loc 22
rs 7.6666

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;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote\Address was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
use Magento\Sales\Model\Order\Address as OrderAddress;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\Address was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $oPayment is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    protected function addUserDataParameters($oBilling, /** @scrutinizer ignore-unused */ PayoneMethod $oPayment, $iGender, $sEmail, $sDob, $blIsUpdateUser = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $this->addAddress($oBilling);
112
113
        if ($iGender || $blIsUpdateUser) {
114
            $this->addParameter('salutation', $this->customerHelper->getSalutationParameter($iGender), $blIsUpdateUser);
0 ignored issues
show
Bug introduced by
$iGender of type string is incompatible with the type integer expected by parameter $iGender of Payone\Core\Helper\Custo...etSalutationParameter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $this->addParameter('salutation', $this->customerHelper->getSalutationParameter(/** @scrutinizer ignore-type */ $iGender), $blIsUpdateUser);
Loading history...
115
            $this->addParameter('gender', $this->customerHelper->getGenderParameter($iGender), $blIsUpdateUser);
0 ignored issues
show
Bug introduced by
$iGender of type string is incompatible with the type integer expected by parameter $iGender of Payone\Core\Helper\Customer::getGenderParameter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
            $this->addParameter('gender', $this->customerHelper->getGenderParameter(/** @scrutinizer ignore-type */ $iGender), $blIsUpdateUser);
Loading history...
116
        }
117
118
        $this->addParameter('email', $sEmail);
119
        if ($blIsUpdateUser || $oBilling->getTelephone()) {
120
            $this->addParameter('telephonenumber', $oBilling->getTelephone(), $blIsUpdateUser);
121
        }
122
123
        // Might be needed again with new Klarna in if-statement: in_array($oPayment->getCode(), [PayoneConfig::METHOD_KLARNA]) && in_array($oBilling->getCountryId(), ['DE', 'NL', 'AT'])
124
        if ($blIsUpdateUser || ($sDob != '0000-00-00 00:00:00' && $sDob != '')) {
125
            $this->addParameter('birthday', str_replace('-', '', date('Ymd', strtotime($sDob)), $blIsUpdateUser));
0 ignored issues
show
Bug introduced by
$blIsUpdateUser of type boolean is incompatible with the type integer|null expected by parameter $count of str_replace(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            $this->addParameter('birthday', str_replace('-', '', date('Ymd', strtotime($sDob)), /** @scrutinizer ignore-type */ $blIsUpdateUser));
Loading history...
126
        }
127
128
        $this->addParameter('language', $this->shopHelper->getLocale());
129
        if ($blIsUpdateUser || $oBilling->getVatId() != '') {
130
            $this->addParameter('vatid', $oBilling->getVatId(), $blIsUpdateUser);
131
        }
132
    }
133
}
134