Addresscheck::isCheckEnabled()   B
last analyzed

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
/**
30
 * Class for the PAYONE Server API request "addresscheck"
31
 */
32
class Addresscheck extends AddressRequest
33
{
34
    /*
35
     * Array of valid countries for addresscheck basic
36
     *
37
     * @var array
38
     */
39
    protected $aValidCountrys = [
40
        'BE',
41
        'DK',
42
        'DE',
43
        'FI',
44
        'FR',
45
        'IT',
46
        'CA',
47
        'LU',
48
        'NL',
49
        'NO',
50
        'AT',
51
        'PL',
52
        'PT',
53
        'SE',
54
        'CH',
55
        'SK',
56
        'ES',
57
        'CZ',
58
        'HU',
59
        'US',
60
    ];
61
    
62
    /**
63
     * Checked addresses resource model
64
     * 
65
     * @var \Payone\Core\Model\ResourceModel\CheckedAddresses 
66
     */
67
    protected $addressesChecked;
68
69
    /**
70
     * Constructor
71
     *
72
     * @param \Payone\Core\Helper\Shop                          $shopHelper
73
     * @param \Payone\Core\Helper\Environment                   $environmentHelper
74
     * @param \Payone\Core\Helper\Api                           $apiHelper
75
     * @param \Payone\Core\Model\ResourceModel\ApiLog           $apiLog
76
     * @param \Payone\Core\Helper\Customer                      $customerHelper
77
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
78
     */
79 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...
80
        \Payone\Core\Helper\Shop $shopHelper,
81
        \Payone\Core\Helper\Environment $environmentHelper,
82
        \Payone\Core\Helper\Api $apiHelper,
83
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
84
        \Payone\Core\Helper\Customer $customerHelper,
85
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
86
    ) {
87
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
88
        $this->addressesChecked = $addressesChecked;
89
    }
90
91
    /**
92
     * Get addresscheck type
93
     *
94
     * @param  bool $blIsBillingAddress
95
     * @return string
96
     */
97
    protected function getAddresscheckType($blIsBillingAddress)
98
    {
99
        $sConfigField = 'check_billing';
100
        if ($blIsBillingAddress === false) {
101
            $sConfigField = 'check_shipping';
102
        }
103
        return $this->shopHelper->getConfigParam($sConfigField, 'address_check', 'payone_protect');
104
    }
105
106
    /**
107
     * Check if the addresscheck is available for the given check-type and address-country
108
     *
109
     * @param  string                                   $sAddresscheckType
110
     * @param  \Magento\Quote\Api\Data\AddressInterface $oAddress
111
     * @return bool
112
     */
113
    protected function validateTypeToCountry($sAddresscheckType, \Magento\Quote\Api\Data\AddressInterface $oAddress)
114
    {
115
        if ($sAddresscheckType == 'PE' && $oAddress->getCountryId() != 'DE') {
116
            //AddressCheck Person only available for germany
117
            return false;
118
        }
119
        if ($sAddresscheckType == 'BA' && array_search($oAddress->getCountryId(), $this->aValidCountrys) === false) {
120
            //AddressCheck Basic only available for certain countries
121
            return false;
122
        }
123
        return true;
124
    }
125
126
    /**
127
     * Check enabled status
128
     *
129
     * @param  bool $blIsBillingAddress
130
     * @return bool
131
     */
132
    protected function isCheckEnabled($blIsBillingAddress)
133
    {
134
        if (!$this->shopHelper->getConfigParam('enabled', 'address_check', 'payone_protect')) {
135
            return false; // address check was disabled
136
        }
137
        if ($blIsBillingAddress && $this->shopHelper->getConfigParam('check_billing', 'address_check', 'payone_protect') == 'NO') {
138
            return false; // address check was disabled for the billing address
139
        }
140
        if (!$blIsBillingAddress && $this->shopHelper->getConfigParam('check_shipping', 'address_check', 'payone_protect') == 'NO') {
141
            return false; // address check was disabled for the shipping address
142
        }
143
        return true;
144
    }
145
146
    /**
147
     * Send request "addresscheck" to PAYONE server API
148
     *
149
     * @param  \Magento\Quote\Api\Data\AddressInterface $oAddress
150
     * @param  bool                                     $blIsBillingAddress
151
     * @return array|bool
152
     */
153
    public function sendRequest(\Magento\Quote\Api\Data\AddressInterface $oAddress, $blIsBillingAddress = false)
154
    {
155
        if (!$this->isCheckEnabled($blIsBillingAddress)) { // check not needed because of configuration
156
            return true;
157
        }
158
159
        $sType = $this->getAddresscheckType($blIsBillingAddress);
160
        if (!$this->validateTypeToCountry($sType, $oAddress)) {
161
            return ['wrongCountry' => true]; //Simulate successful check
162
        }
163
164
        $this->addParameter('request', 'addresscheck');
165
        $this->addParameter('mode', $this->shopHelper->getConfigParam('mode', 'address_check', 'payone_protect')); //Operationmode live or test
166
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); //ID of PayOne Sub-Account
167
        $this->addParameter('addresschecktype', $sType);
168
        $this->addParameter('language', $this->shopHelper->getLocale());
169
        $this->addAddress($oAddress);
170
171
        if ($this->addressesChecked->wasAddressCheckedBefore($oAddress) === false) {
172
            $aResponse = $this->send();
173
            if ($aResponse['status'] == 'VALID') {
174
                $this->addressesChecked->addCheckedAddress($oAddress, $aResponse);
175
            }
176
            return $aResponse;
177
        }
178
        return true;
179
    }
180
}
181