Passed
Push — master ( 1b84e5...95ac4e )
by Florian
42s queued 12s
created

RatepayBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 21
dl 0
loc 27
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 - 2020 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\Methods\Ratepay;
28
29
use Payone\Core\Model\Methods\Payolution\PayolutionBase;
30
use Payone\Core\Model\PayoneConfig;
31
use Payone\Core\Model\Methods\PayoneMethod;
32
use Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order 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...
33
use Magento\Framework\DataObject;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\DataObject 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...
34
35
/**
36
 * Base class for all Ratepay methods
37
 */
38
class RatepayBase extends PayoneMethod
39
{
40
    /* Payment method sub types */
41
    const METHOD_RATEPAY_SUBTYPE_INVOICE = 'RPV';
42
    const METHOD_RATEPAY_SUBTYPE_INSTALLMENT = 'RPS';
43
    const METHOD_RATEPAY_SUBTYPE_DEBIT = 'RPD';
44
45
    /**
46
     * Payone Ratepay helper
47
     *
48
     * @var \Payone\Core\Helper\Ratepay
49
     */
50
    protected $ratepayHelper;
51
52
    /**
53
     * Payone Api helper
54
     *
55
     * @var \Payone\Core\Helper\Api
56
     */
57
    protected $apiHelper;
58
59
    /**
60
     * Payment ban resource model
61
     *
62
     * @var \Payone\Core\Model\ResourceModel\PaymentBan
63
     */
64
    protected $paymentBan;
65
66
    /**
67
     * Clearingtype for PAYONE authorization request
68
     *
69
     * @var string
70
     */
71
    protected $sClearingtype = 'fnc';
72
73
    /**
74
     * Payment method group identifier
75
     *
76
     * @var string
77
     */
78
    protected $sGroupName = PayoneConfig::METHOD_GROUP_RATEPAY;
79
80
    /**
81
     * Info instructions block path
82
     *
83
     * @var string
84
     */
85
    protected $_infoBlockType = 'Payone\Core\Block\Info\ClearingReference';
86
87
    /**
88
     * Keys that need to be assigned to the additionalinformation fields
89
     *
90
     * @var array
91
     */
92
    protected $aAssignKeys = [
93
        'telephone',
94
        'dateofbirth',
95
    ];
96
97
    /**
98
     * Payment ban duration in hours
99
     *
100
     * @var int
101
     */
102
    protected $iBanDuration = 48;
103
104
    /**
105
     * Constructor
106
     *
107
     * @param \Magento\Framework\Model\Context                        $context
108
     * @param \Magento\Framework\Registry                             $registry
109
     * @param \Magento\Framework\Api\ExtensionAttributesFactory       $extensionFactory
110
     * @param \Magento\Framework\Api\AttributeValueFactory            $customAttrFactory
111
     * @param \Magento\Payment\Helper\Data                            $paymentData
112
     * @param \Magento\Framework\App\Config\ScopeConfigInterface      $scopeConfig
113
     * @param \Magento\Payment\Model\Method\Logger                    $logger
114
     * @param \Payone\Core\Helper\Toolkit                             $toolkitHelper
115
     * @param \Payone\Core\Helper\Shop                                $shopHelper
116
     * @param \Magento\Framework\Url                                  $url
117
     * @param \Magento\Checkout\Model\Session                         $checkoutSession
118
     * @param \Payone\Core\Model\Api\Request\Debit                    $debitRequest
119
     * @param \Payone\Core\Model\Api\Request\Capture                  $captureRequest
120
     * @param \Payone\Core\Model\Api\Request\Authorization            $authorizationRequest
121
     * @param \Payone\Core\Model\ResourceModel\SavedPaymentData       $savedPaymentData
122
     * @param \Payone\Core\Helper\Ratepay                             $ratepayHelper
123
     * @param \Payone\Core\Helper\Api                                 $apiHelper
124
     * @param \Payone\Core\Model\ResourceModel\PaymentBan             $paymentBan
125
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
126
     * @param \Magento\Framework\Data\Collection\AbstractDb           $resourceCollection
127
     * @param array                                                   $data
128
     */
129
    public function __construct(
130
        \Magento\Framework\Model\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\Context 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...
131
        \Magento\Framework\Registry $registry,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Registry 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...
132
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Api\ExtensionAttributesFactory 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...
133
        \Magento\Framework\Api\AttributeValueFactory $customAttrFactory,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Api\AttributeValueFactory 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...
134
        \Magento\Payment\Helper\Data $paymentData,
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Helper\Data 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...
135
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Config\ScopeConfigInterface 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...
136
        \Magento\Payment\Model\Method\Logger $logger,
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\Method\Logger 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...
137
        \Payone\Core\Helper\Toolkit $toolkitHelper,
138
        \Payone\Core\Helper\Shop $shopHelper,
139
        \Magento\Framework\Url $url,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Url 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...
140
        \Magento\Checkout\Model\Session $checkoutSession,
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session 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...
141
        \Payone\Core\Model\Api\Request\Debit $debitRequest,
142
        \Payone\Core\Model\Api\Request\Capture $captureRequest,
143
        \Payone\Core\Model\Api\Request\Authorization $authorizationRequest,
144
        \Payone\Core\Model\ResourceModel\SavedPaymentData $savedPaymentData,
145
        \Payone\Core\Helper\Ratepay $ratepayHelper,
146
        \Payone\Core\Helper\Api $apiHelper,
147
        \Payone\Core\Model\ResourceModel\PaymentBan $paymentBan,
148
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\...eModel\AbstractResource 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...
149
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Data\Collection\AbstractDb 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...
150
        array $data = []
151
    ) {
152
        parent::__construct($context, $registry, $extensionFactory, $customAttrFactory, $paymentData, $scopeConfig, $logger, $toolkitHelper, $shopHelper, $url, $checkoutSession, $debitRequest, $captureRequest, $authorizationRequest, $savedPaymentData, $resource, $resourceCollection, $data);
153
        $this->ratepayHelper = $ratepayHelper;
154
        $this->apiHelper = $apiHelper;
155
        $this->paymentBan = $paymentBan;
156
    }
157
158
    /**
159
     * Add the checkout-form-data to the checkout session
160
     *
161
     * @param  DataObject $data
162
     * @return $this
163
     */
164
    public function assignData(DataObject $data)
165
    {
166
        parent::assignData($data);
167
168
        $oInfoInstance = $this->getInfoInstance();
169
        foreach ($this->aAssignKeys as $sKey) {
170
            $sData = $this->toolkitHelper->getAdditionalDataEntry($data, $sKey);
171
            if ($sData) {
172
                $oInfoInstance->setAdditionalInformation($sKey, $sData);
173
            }
174
        }
175
176
        return $this;
177
    }
178
179
    /**
180
     * Return parameters specific to this payment type
181
     *
182
     * @param  Order $oOrder
183
     * @return array
184
     */
185
    public function getPaymentSpecificParameters(Order $oOrder)
186
    {
187
        $aBaseParams = [
188
            'financingtype' => $this->getSubType(),
189
            'api_version' => '3.10',
190
            'add_paydata[customer_allow_credit_inquiry]' => 'yes',
191
            'add_paydata[device_token]' => $this->ratepayHelper->getRatepayDeviceFingerprintToken(),
192
            'add_paydata[merchant_consumer_id]' => $oOrder->getCustomerId(),
193
            'add_paydata[shop_id]' => $this->getShopIdByOrder($oOrder),
194
        ];
195
196
        $sBirthday = $this->getInfoInstance()->getAdditionalInformation('dateofbirth');
197
        if ($sBirthday) {
198
            $aBaseParams['birthday'] = $sBirthday;
199
        }
200
201
        $sTelephone = $this->getInfoInstance()->getAdditionalInformation('telephone');
202
        if ($sTelephone) {
203
            $aBaseParams['telephonenumber'] = $sTelephone;
204
        }
205
206
        $aSubTypeParams = $this->getSubTypeSpecificParameters($oOrder);
207
        $aParams = array_merge($aBaseParams, $aSubTypeParams);
208
        return $aParams;
209
    }
210
211
    /**
212
     * Returns matching Ratepay shop id by given quote
213
     *
214
     * @param  \Magento\Quote\Api\Data\CartInterface $quote
215
     * @return string
216
     * @throws \Magento\Framework\Exception\LocalizedException
217
     */
218
    protected function getShopIdByQuote(\Magento\Quote\Api\Data\CartInterface $quote)
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\Data\CartInterface 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...
219
    {
220
        $sCountryCode = $quote->getShippingAddress()->getCountryId();
221
        $sCurrency = $this->apiHelper->getCurrencyFromQuote($quote);
222
        $dGrandTotal = $this->apiHelper->getQuoteAmount($quote);
223
224
        return $this->ratepayHelper->getRatepayShopId($this->getCode(), $sCountryCode, $sCurrency, $dGrandTotal);
225
    }
226
227
    /**
228
     * Returns matching Ratepay shop id by given quote
229
     *
230
     * @param Order $oOrder
231
     * @return string
232
     * @throws \Magento\Framework\Exception\LocalizedException
233
     */
234
    protected function getShopIdByOrder(Order $oOrder)
235
    {
236
        $sCountryCode = $oOrder->getShippingAddress()->getCountryId();
237
        $sCurrency = $this->apiHelper->getCurrencyFromOrder($oOrder);
238
        $dGrandTotal = $this->apiHelper->getOrderAmount($oOrder);
239
240
        return $this->ratepayHelper->getRatepayShopId($this->getCode(), $sCountryCode, $sCurrency, $dGrandTotal);
241
    }
242
243
    /**
244
     * Check whether payment method can be used
245
     *
246
     * @param \Magento\Quote\Api\Data\CartInterface|null $quote
247
     * @return bool
248
     */
249
    public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
250
    {
251
        $blParentReturn = parent::isAvailable($quote);
252
        if ($blParentReturn === false) {
253
            return false;
254
        }
255
256
        if ($quote === null) {
257
            $quote = $this->checkoutSession->getQuote();
258
        }
259
260
        if ($this->getShopIdByQuote($quote) === false) {
0 ignored issues
show
introduced by
The condition $this->getShopIdByQuote($quote) === false is always false.
Loading history...
261
            return false;
262
        }
263
264
        return $blParentReturn;
265
    }
266
267
    /**
268
     * Perform certain actions with the response
269
     *
270
     * @param  array $aResponse
271
     * @param  Order $oOrder
272
     * @param  float $amount
273
     * @return array
274
     */
275
    protected function handleResponse($aResponse, Order $oOrder, $amount)
276
    {
277
        if (isset($aResponse['status']) && $aResponse['status'] == 'ERROR'
278
            && isset($aResponse['errorcode']) && $aResponse['errorcode'] == '307'
279
        ) {
280
            if (!empty($oOrder->getCustomerId())) {
281
                $this->paymentBan->addPaymentBan($this->getCode(), $oOrder->getCustomerId(), $this->iBanDuration);
282
            } else { // guest checkout
283
                $aBans = $this->checkoutSession->getPayonePaymentBans();
284
                if (!$aBans) {
285
                    $aBans = [];
286
                }
287
                $aBans[$this->getCode()] = $this->paymentBan->getBanEndDate($this->iBanDuration);
288
                $this->checkoutSession->setPayonePaymentBans($aBans);
289
            }
290
        }
291
        return $aResponse;
292
    }
293
294
    /**
295
     * Return capture parameters specific to this payment type
296
     *
297
     * @param  Order $oOrder
298
     * @return array
299
     */
300
    public function getPaymentSpecificCaptureParameters(Order $oOrder)
301
    {
302
        return [
303
            'add_paydata[shop_id]' => $oOrder->getPayoneRatepayShopId()
304
        ];
305
    }
306
307
    /**
308
     * Return debit parameters specific to this payment type
309
     *
310
     * @param  Order $oOrder
311
     * @return array
312
     */
313
    public function getPaymentSpecificDebitParameters(Order $oOrder)
314
    {
315
        return [
316
            'add_paydata[shop_id]' => $oOrder->getPayoneRatepayShopId()
317
        ];
318
    }
319
}
320