Completed
Push — master ( b50753...137f43 )
by Florian
03:57
created

Addresscheck::isCheckEnabled()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 8
nc 4
nop 1
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 Locale;
30
31
/**
32
 * Class for the PAYONE Server API request "addresscheck"
33
 */
34
class Addresscheck extends AddressRequest
35
{
36
    /*
37
     * Array of valid countries for addresscheck basic
38
     *
39
     * @var array
40
     */
41
    protected $aValidCountrys = [
42
        'BE',
43
        'DK',
44
        'DE',
45
        'FI',
46
        'FR',
47
        'IT',
48
        'CA',
49
        'LU',
50
        'NL',
51
        'NO',
52
        'AT',
53
        'PL',
54
        'PT',
55
        'SE',
56
        'CH',
57
        'SK',
58
        'ES',
59
        'CZ',
60
        'HU',
61
        'US',
62
    ];
63
    
64
    /**
65
     * Checked addresses resource model
66
     * 
67
     * @var \Payone\Core\Model\ResourceModel\CheckedAddresses 
68
     */
69
    protected $addressesChecked;
70
71
    /**
72
     * Constructor
73
     *
74
     * @param \Payone\Core\Helper\Shop                          $shopHelper
75
     * @param \Payone\Core\Helper\Environment                   $environmentHelper
76
     * @param \Payone\Core\Helper\Api                           $apiHelper
77
     * @param \Payone\Core\Model\ResourceModel\ApiLog           $apiLog
78
     * @param \Payone\Core\Helper\Customer                      $customerHelper
79
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
80
     */
81 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
        \Payone\Core\Helper\Shop $shopHelper,
83
        \Payone\Core\Helper\Environment $environmentHelper,
84
        \Payone\Core\Helper\Api $apiHelper,
85
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
86
        \Payone\Core\Helper\Customer $customerHelper,
87
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
88
    ) {
89
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
90
        $this->addressesChecked = $addressesChecked;
91
    }
92
93
    /**
94
     * Get addresscheck type
95
     *
96
     * @param  bool $blIsBillingAddress
97
     * @return string
98
     */
99
    protected function getAddresscheckType($blIsBillingAddress)
100
    {
101
        $sConfigField = 'check_billing';
102
        if ($blIsBillingAddress === false) {
103
            $sConfigField = 'check_shipping';
104
        }
105
        return $this->shopHelper->getConfigParam($sConfigField, 'address_check', 'payone_protect');
106
    }
107
108
    /**
109
     * Check if the addresscheck is available for the given check-type and address-country
110
     *
111
     * @param  string                                   $sAddresscheckType
112
     * @param  \Magento\Quote\Api\Data\AddressInterface $oAddress
113
     * @return bool
114
     */
115
    protected function validateTypeToCountry($sAddresscheckType, \Magento\Quote\Api\Data\AddressInterface $oAddress)
116
    {
117
        if ($sAddresscheckType == 'PE' && $oAddress->getCountryId() != 'DE') {
118
            //AddressCheck Person only available for germany
119
            return false;
120
        }
121
        if ($sAddresscheckType == 'BA' && array_search($oAddress->getCountryId(), $this->aValidCountrys) === false) {
122
            //AddressCheck Basic only available for certain countries
123
            return false;
124
        }
125
        return true;
126
    }
127
128
    /**
129
     * Check enabled status
130
     *
131
     * @param  bool $blIsBillingAddress
132
     * @return bool
133
     */
134
    protected function isCheckEnabled($blIsBillingAddress)
135
    {
136
        if (!$this->shopHelper->getConfigParam('enabled', 'address_check', 'payone_protect')) {
137
            return false; // address check was disabled
138
        }
139
        if ($blIsBillingAddress && $this->shopHelper->getConfigParam('check_billing', 'address_check', 'payone_protect') == 'NO') {
140
            return false; // address check was disabled for the billing address
141
        }
142
        if (!$blIsBillingAddress && $this->shopHelper->getConfigParam('check_shipping', 'address_check', 'payone_protect') == 'NO') {
143
            return false; // address check was disabled for the shipping address
144
        }
145
        return true;
146
    }
147
148
    /**
149
     * Send request "addresscheck" to PAYONE server API
150
     *
151
     * @param  \Magento\Quote\Api\Data\AddressInterface $oAddress
152
     * @param  bool                                     $blIsBillingAddress
153
     * @return array|bool
154
     */
155
    public function sendRequest(\Magento\Quote\Api\Data\AddressInterface $oAddress, $blIsBillingAddress = false)
156
    {
157
        if (!$this->isCheckEnabled($blIsBillingAddress)) { // check not needed because of configuration
158
            return true;
159
        }
160
161
        $sType = $this->getAddresscheckType($blIsBillingAddress);
162
        if (!$this->validateTypeToCountry($sType, $oAddress)) {
163
            return ['wrongCountry' => true]; //Simulate successful check
164
        }
165
166
        $this->addParameter('request', 'addresscheck');
167
        $this->addParameter('mode', $this->shopHelper->getConfigParam('mode', 'address_check', 'payone_protect')); //Operationmode live or test
168
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); //ID of PayOne Sub-Account
169
        $this->addParameter('addresschecktype', $sType);
170
        $this->addParameter('language', Locale::getPrimaryLanguage(Locale::getDefault()));
171
        $this->addAddress($oAddress);
172
173 View Code Duplication
        if ($this->addressesChecked->wasAddressCheckedBefore($oAddress) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
            $aResponse = $this->send();
175
            if ($aResponse['status'] == 'VALID') {
176
                $this->addressesChecked->addCheckedAddress($oAddress, $aResponse);
0 ignored issues
show
Bug introduced by
It seems like $aResponse defined by $this->send() on line 174 can also be of type string; however, Payone\Core\Model\Resour...es::addCheckedAddress() does only seem to accept array, 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...
177
            }
178
            return $aResponse;
179
        }
180
        return true;
181
    }
182
}
183