Consumerscore::sendRequest()   C
last analyzed

Complexity

Conditions 12
Paths 29

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 29
nop 3
dl 0
loc 36
rs 6.9666
c 0
b 0
f 0

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 Magento\Quote\Api\Data\AddressInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\Data\AddressInterface 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...
30
use Payone\Core\Model\Source\AddressCheckType;
31
use Payone\Core\Model\Source\CreditratingCheckType;
32
33
/**
34
 * Class for the PAYONE Server API request "consumerscore"
35
 */
36
class Consumerscore extends AddressRequest
37
{
38
39
    /**
40
     * Object of CheckedAddresses resource
41
     *
42
     * @var \Payone\Core\Model\ResourceModel\CheckedAddresses
43
     */
44
    protected $addressesChecked;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param \Payone\Core\Helper\Shop                          $shopHelper
50
     * @param \Payone\Core\Helper\Environment                   $environmentHelper
51
     * @param \Payone\Core\Helper\Api                           $apiHelper
52
     * @param \Payone\Core\Helper\Toolkit                       $toolkitHelper
53
     * @param \Payone\Core\Model\ResourceModel\ApiLog           $apiLog
54
     * @param \Payone\Core\Helper\Customer                      $customerHelper
55
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
56
     */
57
    public function __construct(
58
        \Payone\Core\Helper\Shop $shopHelper,
59
        \Payone\Core\Helper\Environment $environmentHelper,
60
        \Payone\Core\Helper\Api $apiHelper,
61
        \Payone\Core\Helper\Toolkit $toolkitHelper,
62
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
63
        \Payone\Core\Helper\Customer $customerHelper,
64
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
65
    ) {
66
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $toolkitHelper, $apiLog, $customerHelper);
67
        $this->addressesChecked = $addressesChecked;
68
    }
69
70
    /**
71
     * Check enabled status
72
     *
73
     * @return bool
74
     */
75
    protected function isCheckEnabled()
76
    {
77
        if (!$this->shopHelper->getConfigParam('enabled', 'creditrating', 'payone_protect')) {
78
            return false;
79
        }
80
        return true;
81
    }
82
83
    /**
84
     * Get check type for combined address checks but enforce 'Boniversum Person'
85
     * if the configured credit rating check type is 'Boniversum VERITA'
86
     *
87
     * @return string
88
     */
89
    protected function getCombinedAdressCheckType()
90
    {
91
        $creditRatingCheckType = $this->shopHelper->getConfigParam('type', 'creditrating', 'payone_protect');
92
        if ($creditRatingCheckType == CreditratingCheckType::BONIVERSUM_VERITA) {
93
            return AddressCheckType::BONIVERSUM_PERSON;
94
        }
95
        return $this->shopHelper->getConfigParam('addresscheck', 'creditrating', 'payone_protect');
96
    }
97
98
    /**
99
     * Send request "addresscheck" to PAYONE server API
100
     *
101
     * @param  AddressInterface $oAddress
102
     * @param  string $sGender
103
     * @param  string $sBirthday
104
     * @return array|bool
105
     */
106
    public function sendRequest(AddressInterface $oAddress, $sGender = null, $sBirthday = null)
107
    {
108
        if (!$this->isCheckEnabled() || $oAddress->getCountryId() != 'DE') {
109
            return true;
110
        }
111
112
        $sType = $this->shopHelper->getConfigParam('type', 'creditrating', 'payone_protect');
113
114
        $this->addParameter('request', 'consumerscore');
115
        $this->addParameter('mode', $this->shopHelper->getConfigParam('mode', 'creditrating', 'payone_protect')); //Operationmode live or test
116
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); //ID of PayOne Sub-Account
117
        $this->addParameter('addresschecktype', $this->getCombinedAdressCheckType());
118
        $this->addParameter('consumerscoretype', $sType);
119
        $this->addParameter('language', $this->shopHelper->getLocale());
120
121
        if ($sType == CreditratingCheckType::BONIVERSUM_VERITA && $sGender !== null) {
122
            $this->addParameter('gender', $sGender);
123
        }
124
        if ($sType == CreditratingCheckType::BONIVERSUM_VERITA && $sBirthday !== null) {
125
            $this->addParameter('birthday', $sBirthday);
126
        }
127
128
        $this->addAddress($oAddress);
129
        if ($this->addressesChecked->wasAddressCheckedBefore($oAddress, $sType,true) === false) {
130
            $aResponse = $this->send();
131
            if (isset($aResponse['score']) && $aResponse['score'] === 'U') {
132
                $unknownDefault = $this->shopHelper->getConfigParam('unknown_value', 'creditrating', 'payone_protect');
133
                $aResponse['score'] = empty($unknownDefault) ? 'G' : $unknownDefault;
134
            }
135
            if ($aResponse['status'] == 'VALID') {
136
                $this->addressesChecked->addCheckedAddress($oAddress, $aResponse, $sType,true);
137
            }
138
139
            return $aResponse;
140
        }
141
        return true;
142
    }
143
}
144