Consumerscore::sendRequest()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 19
nc 8
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 Magento\Quote\Api\Data\AddressInterface;
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\Model\ResourceModel\ApiLog           $apiLog
53
     * @param \Payone\Core\Helper\Customer                      $customerHelper
54
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
55
     */
56 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...
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
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
63
    ) {
64
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
65
        $this->addressesChecked = $addressesChecked;
66
    }
67
68
    /**
69
     * Check enabled status
70
     *
71
     * @return bool
72
     */
73
    protected function isCheckEnabled()
74
    {
75
        if (!$this->shopHelper->getConfigParam('enabled', 'creditrating', 'payone_protect')) {
76
            return false;
77
        }
78
        return true;
79
    }
80
81
    /**
82
     * Get check type for combined address checks but enforce 'Boniversum Person'
83
     * if the configured credit rating check type is 'Boniversum VERITA'
84
     *
85
     * @return string
86
     */
87
    protected function getCombinedAdressCheckType()
88
    {
89
        $creditRatingCheckType = $this->shopHelper->getConfigParam('type', 'creditrating', 'payone_protect');
90
        if ($creditRatingCheckType == CreditratingCheckType::BONIVERSUM_VERITA) {
91
            return AddressCheckType::BONIVERSUM_PERSON;
92
        }
93
        return $this->shopHelper->getConfigParam('addresscheck', 'creditrating', 'payone_protect');
94
    }
95
96
    /**
97
     * Send request "addresscheck" to PAYONE server API
98
     *
99
     * @param  AddressInterface $oAddress
100
     * @return array|bool
101
     */
102
    public function sendRequest(AddressInterface $oAddress)
103
    {
104
        if (!$this->isCheckEnabled() || $oAddress->getCountryId() != 'DE') {
105
            return true;
106
        }
107
108
        $this->addParameter('request', 'consumerscore');
109
        $this->addParameter('mode', $this->shopHelper->getConfigParam('mode', 'creditrating', 'payone_protect')); //Operationmode live or test
110
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); //ID of PayOne Sub-Account
111
        $this->addParameter('addresschecktype', $this->getCombinedAdressCheckType());
112
        $this->addParameter('consumerscoretype', $this->shopHelper->getConfigParam('type', 'creditrating', 'payone_protect'));
113
        $this->addParameter('language', $this->shopHelper->getLocale());
114
115
        $this->addAddress($oAddress);
116
        if ($this->addressesChecked->wasAddressCheckedBefore($oAddress, true) === false) {
117
            $aResponse = $this->send();
118
            if (isset($aResponse['score']) && $aResponse['score'] === 'U') {
119
                $unknownDefault = $this->shopHelper->getConfigParam('unknown_value', 'creditrating', 'payone_protect');
120
                $aResponse['score'] = empty($unknownDefault) ? 'G' : $unknownDefault;
121
            }
122
            if ($aResponse['status'] == 'VALID') {
123
                $this->addressesChecked->addCheckedAddress($oAddress, $aResponse, true);
124
            }
125
126
            return $aResponse;
127
        }
128
        return true;
129
    }
130
}
131