RatepayProfileConfig::getDataArray()   F
last analyzed

Complexity

Conditions 18
Paths > 20000

Size

Total Lines 63
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 18
eloc 60
c 3
b 0
f 0
nc 131072
nop 1
dl 0
loc 63
rs 0.7

How to fix   Long Method    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 - 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\ResourceModel;
28
29
use Payone\Core\Model\PayoneConfig;
30
31
/**
32
 * Resource model for payone_ratepay_profile_config table
33
 */
34
class RatepayProfileConfig extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\...urceModel\Db\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...
35
{
36
    /**
37
     * Map for methodCode to short payment method identifier for database columns
38
     *
39
     * @var array
40
     */
41
    protected $aMethodIdentifierMap = [
42
        PayoneConfig::METHOD_RATEPAY_INVOICE => 'invoice',
43
        PayoneConfig::METHOD_RATEPAY_DEBIT => 'elv',
44
        PayoneConfig::METHOD_RATEPAY_INSTALLMENT => 'installment',
45
    ];
46
47
    /**
48
     * Initialize connection and table
49
     *
50
     * @return void
51
     */
52
    protected function _construct()
53
    {
54
        $this->_init(\Payone\Core\Setup\Tables\RatepayProfileConfig::TABLE_RATEPAY_PROFILE_CONFIG, 'id');
55
    }
56
57
    /**
58
     * Returns all profile configs
59
     *
60
     * @return array
61
     */
62
    public function getAllProfileConfigs()
63
    {
64
        return $this->getProfileConfigsByIds([]);
65
    }
66
67
    /**
68
     * Get profile configs by given shop ids
69
     *
70
     * @param  array $aShopIds
71
     * @return array
72
     */
73
    public function getProfileConfigsByIds($aShopIds)
74
    {
75
        $oSelect = $this->getConnection()->select()
76
            ->from($this->getMainTable())
77
            ->order('shop_id ASC');
78
79
        if (!empty($aShopIds)) {
80
            $oSelect->where("shop_id IN ('".implode("','", $aShopIds)."')");
81
        }
82
83
        $aResult = $this->getConnection()->fetchAll($oSelect);
84
        return $aResult;
85
    }
86
87
    /**
88
     * Checks if given profile config exists in the database
89
     *
90
     * @param  string $sShopId
91
     * @return bool
92
     */
93
    public function profileExists($sShopId)
94
    {
95
        $aProfileConfigs = $this->getProfileConfigsByIds([$sShopId]);
96
        if (!empty($aProfileConfigs)) {
97
            return true;
98
        }
99
        return false;
100
    }
101
102
    /**
103
     * Fills data array for insert and update queries
104
     *
105
     * @param  array $aProfileResponse
106
     * @return array
107
     */
108
    protected function getDataArray($aProfileResponse)
109
    {
110
        $aData = [
111
            'profile_id'                            => $aProfileResponse['add_paydata[profile-id]'],
112
            'merchant_name'                         => $aProfileResponse['add_paydata[merchant-name]'],
113
            'merchant_status'                       => $aProfileResponse['add_paydata[merchant-status]'],
114
            'shop_name'                             => $aProfileResponse['add_paydata[shop-name]'],
115
            'name'                                  => $aProfileResponse['add_paydata[name]'],
116
            'currency'                              => $aProfileResponse['add_paydata[currency]'],
117
            'type'                                  => $aProfileResponse['add_paydata[type]'],
118
            'activation_status_elv'                 => $aProfileResponse['add_paydata[activation-status-elv]'],
119
            'activation_status_installment'         => $aProfileResponse['add_paydata[activation-status-installment]'],
120
            'activation_status_invoice'             => $aProfileResponse['add_paydata[activation-status-invoice]'],
121
            'activation_status_prepayment'          => $aProfileResponse['add_paydata[activation-status-prepayment]'],
122
            'amount_min_longrun'                    => $aProfileResponse['add_paydata[amount-min-longrun]'],
123
            'b2b_pq_full'                           => $aProfileResponse['add_paydata[b2b-PQ-full]'],
124
            'b2b_pq_light'                          => isset($aProfileResponse['add_paydata[b2b-PQ-light]']) ? $aProfileResponse['add_paydata[b2b-PQ-light]'] : '',
125
            'b2b_elv'                               => $aProfileResponse['add_paydata[b2b-elv]'],
126
            'b2b_installment'                       => $aProfileResponse['add_paydata[b2b-installment]'],
127
            'b2b_invoice'                           => $aProfileResponse['add_paydata[b2b-invoice]'],
128
            'b2b_prepayment'                        => $aProfileResponse['add_paydata[b2b-prepayment]'],
129
            'country_code_billing'                  => $aProfileResponse['add_paydata[country-code-billing]'],
130
            'country_code_delivery'                 => $aProfileResponse['add_paydata[country-code-delivery]'],
131
            'delivery_address_pq_full'              => $aProfileResponse['add_paydata[delivery-address-PQ-full]'],
132
            'delivery_address_pq_light'             => isset($aProfileResponse['add_paydata[delivery-address-PQ-light]']) ? $aProfileResponse['add_paydata[delivery-address-PQ-light]'] : '',
133
            'delivery_address_elv'                  => $aProfileResponse['add_paydata[delivery-address-elv]'],
134
            'delivery_address_installment'          => $aProfileResponse['add_paydata[delivery-address-installment]'],
135
            'delivery_address_invoice'              => $aProfileResponse['add_paydata[delivery-address-invoice]'],
136
            'delivery_address_prepayment'           => $aProfileResponse['add_paydata[delivery-address-prepayment]'],
137
            'device_fingerprint_snippet_id'         => isset($aProfileResponse['add_paydata[device-fingerprint-snippet-id]']) ? $aProfileResponse['add_paydata[device-fingerprint-snippet-id]'] : NULL,
138
            'eligibility_device_fingerprint'        => isset($aProfileResponse['add_paydata[eligibility-device-fingerprint]']) ? $aProfileResponse['add_paydata[eligibility-device-fingerprint]'] : NULL,
139
            'eligibility_ratepay_elv'               => isset($aProfileResponse['add_paydata[eligibility-ratepay-elv]']) ? $aProfileResponse['add_paydata[eligibility-ratepay-elv]'] : 'no',
140
            'eligibility_ratepay_installment'       => isset($aProfileResponse['add_paydata[eligibility-ratepay-installment]']) ? $aProfileResponse['add_paydata[eligibility-ratepay-installment]'] : 'no',
141
            'eligibility_ratepay_invoice'           => isset($aProfileResponse['add_paydata[eligibility-ratepay-invoice]']) ? $aProfileResponse['add_paydata[eligibility-ratepay-invoice]'] : 'no',
142
            'eligibility_ratepay_pq_full'           => isset($aProfileResponse['add_paydata[eligibility-ratepay-pq-full]']) ? $aProfileResponse['add_paydata[eligibility-ratepay-pq-full]'] : 'no',
143
            'eligibility_ratepay_pq_light'          => isset($aProfileResponse['add_paydata[eligibility-ratepay-pq-light]']) ? $aProfileResponse['add_paydata[eligibility-ratepay-pq-light]'] : 'no',
144
            'eligibility_ratepay_prepayment'        => $aProfileResponse['add_paydata[eligibility-ratepay-prepayment]'],
145
            'interest_rate_merchant_towards_bank'   => $aProfileResponse['add_paydata[interest-rate-merchant-towards-bank]'],
146
            'interestrate_default'                  => $aProfileResponse['add_paydata[interestrate-default]'],
147
            'interestrate_max'                      => $aProfileResponse['add_paydata[interestrate-max]'],
148
            'interestrate_min'                      => $aProfileResponse['add_paydata[interestrate-min]'],
149
            'min_difference_dueday'                 => $aProfileResponse['add_paydata[min-difference-dueday]'],
150
            'month_allowed'                         => $aProfileResponse['add_paydata[month-allowed]'],
151
            'month_longrun'                         => $aProfileResponse['add_paydata[month-longrun]'],
152
            'month_number_max'                      => $aProfileResponse['add_paydata[month-number-max]'],
153
            'month_number_min'                      => $aProfileResponse['add_paydata[month-number-min]'],
154
            'payment_amount'                        => $aProfileResponse['add_paydata[payment-amount]'],
155
            'payment_firstday'                      => $aProfileResponse['add_paydata[payment-firstday]'],
156
            'payment_lastrate'                      => $aProfileResponse['add_paydata[payment-lastrate]'],
157
            'rate_min_longrun'                      => $aProfileResponse['add_paydata[rate-min-longrun]'],
158
            'rate_min_normal'                       => $aProfileResponse['add_paydata[rate-min-normal]'],
159
            'service_charge'                        => $aProfileResponse['add_paydata[service-charge]'],
160
            'tx_limit_elv_max'                      => isset($aProfileResponse['add_paydata[tx-limit-elv-max]']) ? $aProfileResponse['add_paydata[tx-limit-elv-max]'] : 0,
161
            'tx_limit_elv_min'                      => isset($aProfileResponse['add_paydata[tx-limit-elv-min]']) ? $aProfileResponse['add_paydata[tx-limit-elv-min]'] : 0,
162
            'tx_limit_installment_max'              => isset($aProfileResponse['add_paydata[tx-limit-installment-max]']) ? $aProfileResponse['add_paydata[tx-limit-installment-max]'] : 0,
163
            'tx_limit_installment_min'              => isset($aProfileResponse['add_paydata[tx-limit-installment-min]']) ? $aProfileResponse['add_paydata[tx-limit-installment-min]'] : 0,
164
            'tx_limit_invoice_max'                  => isset($aProfileResponse['add_paydata[tx-limit-invoice-max]']) ? $aProfileResponse['add_paydata[tx-limit-invoice-max]'] : 0,
165
            'tx_limit_invoice_min'                  => isset($aProfileResponse['add_paydata[tx-limit-invoice-min]']) ? $aProfileResponse['add_paydata[tx-limit-invoice-min]'] : 0,
166
            'tx_limit_prepayment_max'               => isset($aProfileResponse['add_paydata[tx-limit-prepayment-max]']) ? $aProfileResponse['add_paydata[tx-limit-prepayment-max]'] : 0,
167
            'tx_limit_prepayment_min'               => isset($aProfileResponse['add_paydata[tx-limit-prepayment-min]']) ? $aProfileResponse['add_paydata[tx-limit-prepayment-min]'] : 0,
168
            'valid_payment_firstdays'               => $aProfileResponse['add_paydata[valid-payment-firstdays]'],
169
        ];
170
        return $aData;
171
    }
172
173
    /**
174
     * Updates existing profile config in the database
175
     *
176
     * @param  string $sShopId
177
     * @param  array $aProfileResponse
178
     * @return void
179
     */
180
    public function updateProfileConfig($sShopId, $aProfileResponse)
181
    {
182
        $data = $this->getDataArray($aProfileResponse);
183
        $where = ['shop_id = ?' => $sShopId];
184
        $this->getConnection()->update($this->getMainTable(), $data, $where);
185
    }
186
187
    /**
188
     * Insert new line into payone_ratepay_profile_config table
189
     *
190
     * @param  string $sShopId
191
     * @param  array  $aProfileResponse
192
     * @return void
193
     */
194
    public function insertProfileConfig($sShopId, $aProfileResponse)
195
    {
196
        $data = $this->getDataArray($aProfileResponse);
197
        $data['shop_id'] = $sShopId;
198
        $this->getConnection()->insert($this->getMainTable(), $data);
199
    }
200
201
    /**
202
     * Returns ratepay method identifier
203
     *
204
     * @param  string $sMethodCode
205
     * @return string|false
206
     */
207
    protected function getRatepayMethodIdentifierByMethodCode($sMethodCode)
208
    {
209
        if (isset($this->aMethodIdentifierMap[$sMethodCode])) {
210
            return $this->aMethodIdentifierMap[$sMethodCode];
211
        }
212
        return false;
213
    }
214
215
    /**
216
     * Get matching shop id for current quote parameters
217
     *
218
     * @param  string $sMethodCode
219
     * @param  array $aShopIds
220
     * @param  string $sCountryCode
221
     * @param  string $sCurrency
222
     * @param  double $dGrandTotal
223
     * @return string|bool
224
     */
225
    public function getMatchingShopId($sMethodCode, $aShopIds, $sCountryCode, $sCurrency, $dGrandTotal)
226
    {
227
        $sRatepayMethodIdentifier = $this->getRatepayMethodIdentifierByMethodCode($sMethodCode);
228
229
        $oSelect = $this->getConnection()->select()
230
            ->from($this->getMainTable(), ['shop_id'])
231
            ->where("shop_id IN ('".implode("','", $aShopIds)."')")
232
            ->where("tx_limit_".$sRatepayMethodIdentifier."_min <= :grandTotal")
0 ignored issues
show
Bug introduced by
Are you sure $sRatepayMethodIdentifier of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

232
            ->where("tx_limit_"./** @scrutinizer ignore-type */ $sRatepayMethodIdentifier."_min <= :grandTotal")
Loading history...
233
            ->where("tx_limit_".$sRatepayMethodIdentifier."_max >= :grandTotal")
234
            ->where("country_code_billing = :countryCode")
235
            ->where("currency = :currency")
236
            ->order('shop_id ASC')
237
            ->limit(1);
238
239
        $aParams = [
240
            'grandTotal' => $dGrandTotal,
241
            'countryCode' => $sCountryCode,
242
            'currency' => $sCurrency,
243
        ];
244
245
        $sShopId = $this->getConnection()->fetchOne($oSelect, $aParams);
246
        if (empty($sShopId)) {
247
            return false;
248
        }
249
        return $sShopId;
250
    }
251
}
252