Completed
Pull Request — master (#981)
by wiese
110:54 queued 45:53
created

  F

Complexity

Conditions 17
Paths 321

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
c 0
b 0
f 0
nc 321
dl 0
loc 25
rs 3.6909
nop 1

How to fix   Complexity   

Complexity

Complex classes like PaymentSummaryDisplayHandler.getAddressSummaryContent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
'use strict';
2
var objectAssign = require( 'object-assign' ),
3
  PaymentSummaryDisplayHandler = {
4
    intervalElement: null,
5
    amountElement: null,
6
    paymentTypeElement: null,
7
    intervalTranslations: null,
8
    paymentTypeTranslations: null,
9
    numberFormatter: null,
10
    intervalIconElement: null,
11
    intervalIcons: null,
12
    paymentIconsElement: null,
13
    paymentIcons: null,
14
    periodicityTextElement: null,
15
    periodicityText: null,
16
    paymentElement: null,
17
    paymentText: null,
18
    addressTypeIconElement: null,
19
    addressTypeIcon: null,
20
    addressTypeElement: null,
21
    addressType: null,
22
    addressTypeTextElement: null,
23
    countriesDictionary: {'DE': 'Deutschland', 'AT': 'Österreich', 'CH': 'Schweiz', 'BE': 'Belgien', 'IT': 'Italien', 'LU': 'Luxemburg'},
24
    intervalTextElement: null,
25
    intervalText: null,
26
    memberShipTypeElement: null,
27
    memberShipType: null,
28
    memberShipTypeIconElement: null,
29
    memberShipTypeIcon: null,
30
    memberShipTypeTextElement: null,
31
    memberShipTypeText: null,
32
    update: function ( formContent ) {
33
      this.intervalElement.text( this.formatPaymentInterval( formContent.paymentIntervalInMonths ) );
34
      this.intervalTextElement.text( this.intervalText[formContent.paymentIntervalInMonths] );
35
36
      var amountFormat = this.numberFormatter.format(formContent.amount);
37
      this.amountElement.each(function () {
38
        if (formContent.amount > 0 || $(this).data('display-error') === undefined) {
39
          $(this).text(amountFormat);
40
        }
41
        else {
42
          $(this).text('Betrag');
43
        }
44
      });
45
46
      var paymentTypeText = this.formatPaymentType( formContent.paymentType );
47
      this.paymentTypeElement.each(function () {
48
        if (formContent.paymentType !== "" || $(this).data('display-error') === undefined) {
49
          $(this).text(paymentTypeText);
50
        }
51
        else {
52
          $(this).text('Zahlart');
53
        }
54
      });
55
56
      this.setSummaryIcon(this.intervalIconElement, formContent.paymentIntervalInMonths, this.intervalIcons);
57
      this.setSummaryIcon(this.paymentIconsElement, formContent.paymentType, this.paymentIcons);
58
      this.periodicityTextElement.text( this.periodicityText[formContent.paymentIntervalInMonths] );
59
60
      var paymentTextFormatted = this.paymentText[formContent.paymentType];
61
      if (formContent.paymentType == "BEZ") {
62
        paymentTextFormatted = '<div class="col-lg-6 no-gutter">' + paymentTextFormatted + "</div>";
63
        paymentTextFormatted = paymentTextFormatted.replace('<br />', '</div><div class="col-lg-6 no-gutter">');
64
65
        if (formContent.accountNumber && formContent.bankCode) {
66
          paymentTextFormatted = "<dl class='bank-info'><div><dt>Kontonummer</dt><dd>"+formContent.accountNumber+"</dd></div>" +
67
            "<div><dt>Bankleitzahl</dt><dd>"+formContent.bankCode+"</dd></div></dl>" + paymentTextFormatted;
68
        }
69
        else if (formContent.iban && formContent.bic) {
70
          paymentTextFormatted = "<dl class='bank-info'><div><dt>IBAN</dt><dd>"+formContent.iban+"</dd></div>" +
71
          "<div><dt>BIC</dt><dd>"+formContent.bic+"</dd></div></dl>" + paymentTextFormatted;
72
        }
73
      }
74
      this.paymentElement.html(paymentTextFormatted);
75
      this.setSummaryIcon(this.addressTypeIconElement, formContent.addressType, this.addressTypeIcon);
76
77
      if (formContent.addressType != "") {
78
        this.addressTypeElement.text(this.addressType[formContent.addressType]);
79
      }
80
      else {
81
        this.addressTypeElement.each(function () {
82
          if ($(this).data('display-error') === undefined) {
83
            $(this).text("Daten noch nicht ausgewählt.");
84
          }
85
        });
86
      }
87
88
      this.addressTypeTextElement.html(this.getAddressSummaryContent(formContent));
89
90
      if (this.memberShipTypeElement) {
91
        var textMemberShipType = this.memberShipType[formContent.membershipType];
92
        this.memberShipTypeElement.each(function () {
93
          if (formContent.membershipType) {
94
            $(this).text(textMemberShipType);
95
          }
96
        });
97
98
        this.setSummaryIcon(this.memberShipTypeIconElement, formContent.membershipType, this.memberShipTypeIcon);
99
100
        this.memberShipTypeTextElement.text(this.memberShipTypeText[formContent.membershipType]);
101
      }
102
    },
103
    capitalize: function (s) {
104
      return s[0].toUpperCase() + s.slice(1);
105
    },
106
    formatPaymentInterval: function ( paymentIntervalInMonths ) {
107
      return this.intervalTranslations[ paymentIntervalInMonths ];
108
    },
109
    formatPaymentType: function ( paymentType ) {
110
      return paymentType=="" ? String("Zahlung noch nicht ausgewählt.") : this.paymentTypeTranslations[ paymentType ];
111
    },
112
    getAddressSummaryContent: function (formContent) {
113
      if (formContent.addressType === "person") {
114
        return (
115
          formContent.firstName && formContent.lastName ?
116
            (formContent.salutation ? this.capitalize(formContent.salutation) : "") + " " +
117
            (formContent.title ? this.capitalize(formContent.title) : "") + " " +
118
            formContent.firstName + " " + formContent.lastName + "<br />"
119
            : ""
120
        ) +
121
        (formContent.street ? formContent.street + "<br />" : "") +
122
        (formContent.postcode && formContent.city ? formContent.postcode + " " + formContent.city + "<br />" : "") +
123
        ( formContent.country ? this.countriesDictionary[formContent.country] + "<br />" : "") +
124
        formContent.email;
125
      }
126
      else if (formContent.addressType === 'firma') {
127
        return (formContent.companyName ? formContent.companyName + "<br />" : "") +
128
        (formContent.contactPerson ? formContent.contactPerson + "<br />" : "") +
129
        (formContent.street ? formContent.street + "<br />" : "") +
130
        (formContent.postcode && formContent.city ? formContent.postcode + " " + formContent.city + "<br />" : "") +
131
        ( formContent.country ? this.countriesDictionary[formContent.country] + "<br />" : "") +
132
        formContent.email;
133
      }
134
135
      return "";
136
    },
137
    setSummaryIcon: function (elements, value, iconsDictionary) {
138
      elements.removeClass('icon-error');
139
      if (elements.length && elements.get(0).className.split(' ').length > 1) {
140
        elements.removeClass(elements.get(0).className.split(' ').pop());
141
      }
142
      if (iconsDictionary[value] === undefined) {
143
        elements.each(function() {
144
          if ($(this).data('display-error') === undefined) {
145
            $(this).addClass('icon-error');
146
          }
147
        });
148
      }
149
      else {
150
        elements.addClass( iconsDictionary[value] );
151
      }
152
    }
153
  };
154
155
module.exports = {
156
  createPaymentSummaryDisplayHandler: function ( intervalElement, amountElement, paymentTypeElement,
157
                                                 intervalTranslations, paymentTypeTranslations, numberFormatter,
158
                                                 intervalIconElement, intervalIcons, paymentIconsElement, paymentIcons,
159
                                                 periodicityTextElement, periodicityText, paymentElement, paymentText,
160
                                                 addressTypeIconElement, addressTypeIcon, addressTypeElement, addressType,
161
                                                 addressTypeTextElement, intervalTextElement, intervalText,
162
                                                 memberShipTypeElement, memberShipType, memberShipTypeIconElement,
163
                                                 memberShipTypeIcon, memberShipTypeTextElement, memberShipTypeText) {
164
    return objectAssign( Object.create( PaymentSummaryDisplayHandler ), {
165
      intervalElement: intervalElement,
166
      amountElement: amountElement,
167
      paymentTypeElement: paymentTypeElement,
168
      intervalTranslations: intervalTranslations,
169
      paymentTypeTranslations: paymentTypeTranslations,
170
      numberFormatter: numberFormatter,
171
      intervalIconElement: intervalIconElement,
172
      intervalIcons: intervalIcons,
173
      paymentIconsElement: paymentIconsElement,
174
      paymentIcons: paymentIcons,
175
      periodicityTextElement: periodicityTextElement,
176
      periodicityText: periodicityText,
177
      paymentElement: paymentElement,
178
      paymentText: paymentText,
179
      addressTypeIconElement: addressTypeIconElement,
180
      addressTypeIcon: addressTypeIcon,
181
      addressTypeElement: addressTypeElement,
182
      addressType: addressType,
183
      addressTypeTextElement: addressTypeTextElement,
184
      intervalTextElement: intervalTextElement,
185
      intervalText: intervalText,
186
      memberShipTypeElement: memberShipTypeElement,
187
      memberShipType: memberShipType,
188
      memberShipTypeIconElement: memberShipTypeIconElement,
189
      memberShipTypeIcon: memberShipTypeIcon,
190
      memberShipTypeTextElement: memberShipTypeTextElement,
191
      memberShipTypeText: memberShipTypeText
192
    } );
193
  }
194
};
195