Completed
Push — master ( 641a91...fc9f6a )
by
unknown
10:16
created

PassmanImporter.padlock.readFile   D

Complexity

Conditions 12
Paths 19

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 12
dl 0
loc 57
rs 4.7672
c 1
b 1
f 0
nc 19
nop 0

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:

Complexity

Complex classes like PassmanImporter.padlock.readFile 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
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @copyright Copyright (c) 2017, Bingen Eguzkitza ([email protected])
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
// TODO: Allow for new lines and commas in values
25
26
// Importers should always start with this
27
/** global: PassmanImporter */
28
var PassmanImporter = PassmanImporter || {};
29
(function(window, $, PassmanImporter) {
30
	'use strict';
31
	// Define the importer
32
	PassmanImporter.padlock = {
33
		info: {
34
			name: 'Padlock',
35
			id: 'padlock',
36
			exportSteps: ['Create a csv export. Go to Menu -> Settings -> Export Data and copy text into a .csv file']
37
		}
38
	};
39
40
	PassmanImporter.padlock.readFile = function (file_data) {
41
		/** global: C_Promise */
42
		return new C_Promise(function(){
43
			var rows = PassmanImporter.readCsv(file_data, true);
44
			var credential_list = [];
45
			for (var i = 0; i < rows.length; i++) {
46
				var row = rows[i];
47
				var _credential = PassmanImporter.newCredential();
48
				var j = 0;
49
				for (var k in row) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
50
					if (!row[k]) {
51
						continue;
52
					}
53
					if (k == 'name') {
54
						_credential.label = row.name;
55
						continue;
56
					}
57
					if (k == 'username') {
58
						_credential.username = row.username;
59
						continue;
60
					}
61
					if (k == 'password') {
62
						_credential.password = row.password;
63
						continue;
64
					}
65
					if (k.toLowerCase() == 'url') {
66
						_credential.url = row[k];
67
						continue;
68
					}
69
					if (k.toLowerCase() == 'e-mail' ||
70
					    k.toLowerCase() == 'email') {
71
						_credential.email = row[k];
72
						continue;
73
					}
74
					if (k.toLowerCase() == 'description') {
75
						_credential.description = row[k];
76
						continue;
77
					}
78
					_credential.custom_fields[j] = {
79
						'label' : k,
80
						'value' : row[k],
81
						'secret' : true,
82
						'field_type' : 'text'
83
					};
84
					j++;
85
				}
86
				if(_credential.label){
87
					credential_list.push(_credential);
88
				}
89
90
				var progress = {
91
					percent: i/rows.length*100,
92
					loaded: i,
93
					total: rows.length
94
				};
95
				this.call_progress(progress);
96
			}
97
			this.call_then(credential_list);
98
		});
99
	};
100
})(window, $, PassmanImporter);
101