Completed
Pull Request — master (#309)
by Sander
04:26
created

PassmanExporter.getCredentialsWithFiles   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 69
rs 8.56

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
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
// Importers should always start with this
24
if (!window['PassmanExporter']) {
0 ignored issues
show
Coding Style introduced by
['PassmanExporter'] could be written in dot notation.

You can rewrite this statement in dot notation:

var obj = { };
obj['foo'] = 'bar'; // Bad
obj.foo = 'bar'; // Good
Loading history...
25
	var PassmanExporter = {
26
	    getCredentialsWithFiles: function(credentials, FileService, EncryptService) {
27
		var t = {
28
		    cred: credentials,
29
		    FS: FileService,
30
		    ES: EncryptService
31
		};
32
		/** global: C_Promise */
33
		return new C_Promise(function() {
34
		    var _this = this.parent;
35
		    var credentials = _this.cred;
36
		    this.parent.total = 0;
37
		    this.parent.finished = 0;
38
		    this.parent.fileGUID_cred = [];
39
		    this.parent.files = [];
40
		    this.parent.step = (function(file) {
41
			this.parent.finished ++;
42
			this.call_progress({
43
			    total: this.parent.total,
44
			    finished: this.parent.finished
45
			});
46
			
47
			var dta = this.parent.fileGUID_cred[file.guid];
48
			
49
			file.filename = this.parent.ES.decryptString(file.filename, this.parent.cred[dta.cred_pos].vault_key);
50
			file.file_data = this.parent.ES.decryptString(file.file_data, this.parent.cred[dta.cred_pos].vault_key);
51
			
52
			// Files and custom_fields have different field structure
53
			if (dta.on === 'files') {
54
			    this.parent.cred[dta.cred_pos][dta.on][dta.at] = file;
55
			}
56
			else {
57
			    this.parent.cred[dta.cred_pos][dta.on][dta.at].value = file;
58
			}
59
			
60
			// We have finished downloading everything, so let's hand over job to somewhere else!
61
			if (this.parent.total === this.parent.finished) {
62
			    this.call_then(this.parent.cred);
63
			}
64
		    }).bind(this);
65
66
		    for (var i = 0; i < credentials.length; i++) {
67
			
68
			var item = credentials[i];
69
			
70
			// Custom fields
71
			for (c = 0; c < item.custom_fields.length; c++) {
72
			    var cf = item.custom_fields[c];
73
			    if (cf.field_type === 'file') {
74
				this.parent.total ++;
75
				this.parent.fileGUID_cred[cf.value.guid] = {
76
				    cred_pos: i,
77
				    on: 'custom_fields',
78
				    at: c
79
				};
80
81
				this.parent.FS.getFile(cf.value).then((function(data){
82
				    this.parent.step(data);
83
				}).bind(this));
84
			    }
85
			}
86
			
87
			// Also get all files
88
			for (var c = 0; c < item.files.length; c++) {
89
			    this.parent.total ++;
90
			    this.parent.fileGUID_cred[item.files[c].guid] = {
91
				cred_pos: i,
92
				on: 'files',
93
				at: c
94
			    };
95
96
			    this.parent.FS.getFile(item.files[c]).then((function(data){
97
				this.parent.step(data);
98
			    }).bind(this));
99
			}
100
		    }
101
		}, t);
102
	    }
103
	};
104
}
105