Completed
Push — master ( 861445...fc7d9d )
by Florian
9s
created

payolution_installment-method.js ➔ define   C

Complexity

Conditions 1
Paths 768

Size

Total Lines 136

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 768
nop 7
dl 0
loc 136
rs 6.5714

12 Functions

Rating   Name   Duplication   Size   Complexity  
A payolution_installment-method.js ➔ ... ➔ Component.extend.removePayolutionOverlay 0 3 1
B payolution_installment-method.js ➔ ... ➔ Component.extend.getCleanedNumber 0 14 6
A payolution_installment-method.js ➔ ... ➔ Component.extend.getData 0 20 4
A payolution_installment-method.js ➔ ... ➔ Component.extend.getPrivacyDeclaration 0 3 1
A payolution_installment-method.js ➔ ... ➔ Component.extend.initObservable 0 13 1
A payolution_installment-method.js ➔ ... ➔ Component.extend.displayPayolutionOverlay 0 3 1
A payolution_installment-method.js ➔ ... ➔ Component.extend.requestBirthday 0 3 1
A payolution_installment-method.js ➔ ... ➔ Component.extend.getInstructions 0 3 1
B payolution_installment-method.js ➔ ... ➔ Component.extend.isB2bMode 0 10 5
C payolution_installment-method.js ➔ ... ➔ Component.extend.validate 0 23 10
A payolution_installment-method.js ➔ ... ➔ Component.extend.switchInstallmentPlan 0 16 1
A payolution_installment-method.js ➔ ... ➔ Component.extend.handleInstallment 0 9 3

How to fix   Long Method   

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
/**
2
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
3
 * it under the terms of the GNU Lesser General Public License as published by
4
 * the Free Software Foundation, either version 3 of the License, or
5
 * (at your option) any later version.
6
 *
7
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU Lesser General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU Lesser General Public License
13
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
14
 *
15
 * PHP version 5
16
 *
17
 * @category  Payone
18
 * @package   Payone_Magento2_Plugin
19
 * @author    FATCHIP GmbH <[email protected]>
20
 * @copyright 2003 - 2017 Payone GmbH
21
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
22
 * @link      http://www.payone.de
23
 */
24
define(
25
    [
26
        'Payone_Core/js/view/payment/method-renderer/base',
27
        'jquery',
28
        'mage/translate',
29
        'Magento_Checkout/js/model/quote',
30
        'Magento_Checkout/js/model/url-builder',
31
        'Payone_Core/js/action/installmentplan',
32
        'Magento_Checkout/js/model/payment/additional-validators',
33
        'prototype'
34
    ],
35
    function (Component, $, $t, quote, urlBuilder, installmentplan, additionalValidators) {
36
        'use strict';
37
        return Component.extend({
38
            defaults: {
39
                template: 'Payone_Core/payment/payolution_installment',
40
                birthday: '',
41
                birthmonth: '',
42
                birthyear: '',
43
                tradeRegistryNumber: '',
44
                iban: '',
45
                bic: '',
46
                agreement: false
47
            },
48
            initObservable: function () {
49
                this._super()
50
                    .observe([
51
                        'birthday',
52
                        'birthmonth',
53
                        'birthyear',
54
                        'tradeRegistryNumber',
55
                        'iban',
56
                        'bic',
57
                        'agreement'
58
                    ]);
59
                return this;
60
            },
61
            getData: function () {
62
                document.getElementById(this.getCode() + '_iban').value = this.getCleanedNumber(this.iban());
63
                document.getElementById(this.getCode() + '_bic').value = this.getCleanedNumber(this.bic());
64
65
                var parentReturn = this._super();
66
                if (parentReturn.additional_data === null) {
67
                    parentReturn.additional_data = {};
68
                }
69
                if (this.requestBirthday()) {
70
                    parentReturn.additional_data.dateofbirth = this.birthyear() + this.birthmonth() + this.birthday();
71
                }
72
                if (this.isB2bMode()) {
73
                    parentReturn.additional_data.trade_registry_number = this.tradeRegistryNumber();
74
                    parentReturn.additional_data.b2bmode = true;
75
                }
76
                parentReturn.additional_data.iban = this.getCleanedNumber(this.iban());
77
                parentReturn.additional_data.bic = this.getCleanedNumber(this.bic());
78
                parentReturn.additional_data.duration = $('#' + this.getCode() + '_duration').val();
79
                return parentReturn;
80
            },
81
            getCleanedNumber: function (sDirtyNumber) {
82
                var sCleanedNumber = '';
83
                var sTmpChar;
84
                for (var i = 0; i < sDirtyNumber.length; i++) {
85
                    sTmpChar = sDirtyNumber.charAt(i);
86
                    if (sTmpChar != ' ' && (!isNaN(sTmpChar) || /^[A-Za-z]/.test(sTmpChar))) {
87
                        if (/^[a-z]/.test(sTmpChar)) {
88
                            sTmpChar = sTmpChar.toUpperCase();
89
                        }
90
                        sCleanedNumber = sCleanedNumber + sTmpChar;
91
                    }
92
                }
93
                return sCleanedNumber;
94
            },
95
            /** Returns payment method instructions */
96
            getInstructions: function () {
97
                return window.checkoutConfig.payment.instructions[this.item.method];
98
            },
99
            displayPayolutionOverlay: function () {
100
                $('#' + this.getCode() + '_overlay').show();
101
            },
102
            removePayolutionOverlay: function () {
103
                $('#' + this.getCode() + '_overlay').hide();
104
            },
105
            getPrivacyDeclaration: function () {
106
                return window.checkoutConfig.payment.payone.payolution.privacyDeclaration.invoice;
107
            },
108
            isB2bMode: function () {
109
                if (window.checkoutConfig.payment.payone.payolution.b2bMode.invoice == true &&
110
                    quote.billingAddress() != null &&
111
                    typeof quote.billingAddress().company != 'undefined' &&
112
                    quote.billingAddress().company != ''
113
                ) {
114
                    return true;
115
                }
116
                return false;
117
            },
118
            requestBirthday: function () {
119
                return !this.isB2bMode();
120
            },
121
            validate: function () {
122
                if (this.requestBirthday() == true && !this.isBirthdayValid(this.birthyear(), this.birthmonth(), this.birthday())) {
123
                    this.messageContainer.addErrorMessage({'message': $t('You have to be at least 18 years old to use this payment type!')});
124
                    return false;
125
                }
126
                if (this.agreement() == false) {
127
                    this.messageContainer.addErrorMessage({'message': $t('Please confirm the transmission of the necessary data to payolution!')});
128
                    return false;
129
                }
130
                if ($('#' + this.getCode() + '_installmentplan').html() != '' && $('#' + this.getCode() + '_duration').val() == '') {
131
                    this.messageContainer.addErrorMessage({'message': $t('Please select your desired number of installments')});
132
                    return false;
133
                }
134
                if ($('#' + this.getCode() + '_duration').val() != '' && this.iban() == '') {
135
                    this.messageContainer.addErrorMessage({'message': $t('Please enter a valid IBAN.')});
136
                    return false;
137
                }
138
                if ($('#' + this.getCode() + '_duration').val() != '' &&this.bic() == '') {
139
                    this.messageContainer.addErrorMessage({'message': $t('Please enter a valid BIC.')});
140
                    return false;
141
                }
142
                return true;
143
            },
144
            handleInstallment: function () {
145
                if (this.validate() && additionalValidators.validate()) {
146
                    window.payolution_installment = this;
147
                    window.switchInstallmentPlan = window.switchInstallmentPlan || function (sKey, sCode, iInstallments) {
148
                            window.payolution_installment.switchInstallmentPlan(sKey, sCode, iInstallments);
149
                        }
150
                    installmentplan(this, '19601212');
151
                }
152
            },
153
            switchInstallmentPlan: function (sKey, sCode, iInstallments) {
154
                $$('.payolution_installmentplans').each(
155
                    function (e) {
156
                        e.hide();
157
                    }
158
                );
159
                $$('.payolution_installment_overview').each(
160
                    function (e) {
161
                        e.hide();
162
                    }
163
                );
164
165
                $('#payolution_installmentplan_' + sKey).show();
166
                $('#payolution_installment_overview_' + sKey).show();
167
                $('#' + sCode + '_duration').val(iInstallments);
168
            }
169
        });
170
    }
171
);
172