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

Consumerscore::sendRequest()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 8
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 8
loc 24
c 0
b 0
f 0
rs 8.5125
cc 5
eloc 16
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
use Magento\Quote\Api\Data\AddressInterface;
31
32
/**
33
 * Class for the PAYONE Server API request "consumerscore"
34
 */
35
class Consumerscore extends AddressRequest
36
{
37
38
    /**
39
     * Object of CheckedAddresses resource
40
     *
41
     * @var \Payone\Core\Model\ResourceModel\CheckedAddresses
42
     */
43
    protected $addressesChecked;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param \Payone\Core\Helper\Shop                          $shopHelper
49
     * @param \Payone\Core\Helper\Environment                   $environmentHelper
50
     * @param \Payone\Core\Helper\Api                           $apiHelper
51
     * @param \Payone\Core\Model\ResourceModel\ApiLog           $apiLog
52
     * @param \Payone\Core\Helper\Customer                      $customerHelper
53
     * @param \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
54
     */
55 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...
56
        \Payone\Core\Helper\Shop $shopHelper,
57
        \Payone\Core\Helper\Environment $environmentHelper,
58
        \Payone\Core\Helper\Api $apiHelper,
59
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
60
        \Payone\Core\Helper\Customer $customerHelper,
61
        \Payone\Core\Model\ResourceModel\CheckedAddresses $addressesChecked
62
    ) {
63
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog, $customerHelper);
64
        $this->addressesChecked = $addressesChecked;
65
    }
66
67
    /**
68
     * Check enabled status
69
     *
70
     * @return bool
71
     */
72
    protected function isCheckEnabled()
73
    {
74
        if (!$this->shopHelper->getConfigParam('enabled', 'creditrating', 'payone_protect')) {
75
            return false;
76
        }
77
        return true;
78
    }
79
80
    /**
81
     * Send request "addresscheck" to PAYONE server API
82
     *
83
     * @param  AddressInterface $oAddress
84
     * @return array|bool
85
     */
86
    public function sendRequest(AddressInterface $oAddress)
87
    {
88
        if (!$this->isCheckEnabled() || $oAddress->getCountryId() != 'DE') {
89
            return true;
90
        }
91
92
        $this->addParameter('request', 'consumerscore');
93
        $this->addParameter('mode', $this->shopHelper->getConfigParam('mode', 'creditrating', 'payone_protect')); //Operationmode live or test
94
        $this->addParameter('aid', $this->shopHelper->getConfigParam('aid')); //ID of PayOne Sub-Account
95
        $this->addParameter('addresschecktype', $this->shopHelper->getConfigParam('addresscheck', 'creditrating', 'payone_protect'));
96
        $this->addParameter('consumerscoretype', $this->shopHelper->getConfigParam('type', 'creditrating', 'payone_protect'));
97
        $this->addParameter('language', Locale::getPrimaryLanguage(Locale::getDefault()));
98
99
        $this->addAddress($oAddress);
100 View Code Duplication
        if ($this->addressesChecked->wasAddressCheckedBefore($oAddress, true) === 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...
101
            $aResponse = $this->send();
102
            if ($aResponse['status'] == 'VALID') {
103
                $this->addressesChecked->addCheckedAddress($oAddress, $aResponse, true);
0 ignored issues
show
Bug introduced by
It seems like $aResponse defined by $this->send() on line 101 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...
104
            }
105
106
            return $aResponse;
107
        }
108
        return true;
109
    }
110
}
111