Completed
Push — master ( 1a26ad...341936 )
by Florian
04:37
created

Consumerscore::sendRequest()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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