Completed
Push — master ( b8d05c...c84308 )
by wiese
269:29 queued 204:24
created

module.exports.formContent   D

Complexity

Conditions 10
Paths 55

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
nc 55
dl 0
loc 46
rs 4.983
c 1
b 0
f 0
nop 2

How to fix   Complexity   

Complexity

Complex classes like module.exports.formContent 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
3
var objectAssign = require( 'object-assign' ),
4
  _ = require( 'underscore' );
5
6
/**
7
 * Return object keys that are not defined in initial state
8
 *
9
 * @param {Object} state
10
 * @param {Object} initialState
11
 * @returns {Array}
12
 */
13
function getInvalidKeys( state, initialState ) {
14
  return _.keys( _.omit( state, _.keys( initialState ) ) );
15
}
16
17
function setPaymentType(newState, payload) {
18
  if (typeof payload.value !== 'string') {
19
    return;
20
  }
21
  if ( (payload.contentName === 'iban' || payload.contentName === 'bic') && trimValue(payload.value) ) {
22
    newState.debitType = "sepa";
23
  }
24
  else if ( (payload.contentName !== 'accountNumber' || payload.contentName === 'bankCode') && trimValue(payload.value)) {
25
    newState.debitType = "non-sepa";
26
  }
27
}
28
29
function forcePersonalDataForDirectDebit( state ) {
30
  if ( state.paymentType === 'BEZ' && state.addressType === 'anonym' ) {
31
    return objectAssign( {}, state, { addressType: 'person' } );
32
  } else {
33
    return state;
34
  }
35
}
36
37
function forceAddressTypeForActiveMembership( state ) {
38
  if ( state.membershipType === 'active' ) {
39
    return objectAssign( {}, state, { addressType: 'person' } );
40
  } else {
41
    return state;
42
  }
43
}
44
45
function trimValue( value ) {
46
  return value.replace( /^\s+|\s+$/gm, '' );
47
}
48
49
module.exports = {
50
  stateContainsUnknownKeys: function ( state, initialState ) {
51
    return !_.isEmpty( getInvalidKeys( state, initialState ) );
52
  },
53
  getInvalidKeys: getInvalidKeys,
54
  formContent: function ( state, action ) {
55
    var newAmount, newState;
56
    switch ( action.type ) {
57
      case 'SELECT_AMOUNT':
58
        newAmount = action.payload.amount === null ? state.amount : action.payload.amount;
59
        return objectAssign( {}, state, {
60
          amount: newAmount,
61
          isCustomAmount: false
62
        } );
63
      case 'INPUT_AMOUNT':
64
        return objectAssign( {}, state, {
65
          amount: action.payload.amount,
66
          isCustomAmount: true
67
        } );
68
      case 'CHANGE_CONTENT':
69
        if ( !_.has( state, action.payload.contentName ) ) {
70
          throw new Error( 'Unsupported form content name: ' + action.payload.contentName );
71
        }
72
        newState = _.clone( state );
73
74
        if ( _.isString( action.payload.value ) ) {
75
          newState[ action.payload.contentName ] = trimValue( action.payload.value );
76
        } else {
77
          newState[ action.payload.contentName ] = action.payload.value;
78
        }
79
80
        setPaymentType(newState, action.payload);
81
82
        newState = forcePersonalDataForDirectDebit( newState );
83
        newState = forceAddressTypeForActiveMembership( newState );
84
        return newState;
85
      case 'FINISH_BANK_DATA_VALIDATION':
86
        if ( action.payload.status !== 'OK' ) {
87
          return state;
88
        }
89
        return objectAssign( {}, state, {
90
          iban: action.payload.iban || '',
91
          bic: action.payload.bic || state.bic || '',
92
          accountNumber: action.payload.account || '',
93
          bankCode: action.payload.bankCode || '',
94
          bankName: action.payload.bankName || ''
95
        } );
96
      default:
97
        return state;
98
    }
99
  }
100
};
101