Test Setup Failed
Push — master ( 8ee19e...25278d )
by recca
05:07 queued 46s
created

resources/assets/js/commands/command.js   A

Size

Lines of Code 65

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
nc 1
dl 0
loc 65
rs 10
c 1
b 0
f 0
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A command.js ➔ ??? 0 6 1
1
import '../jquery';
2
import '../polyfill';
3
4
export default class Command {
5
    constructor(api, options) {
0 ignored issues
show
Unused Code introduced by
The parameter options is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
6
        Object.assign(this, {
7
            api: api,
8
            requestId: 0
9
        });
10
    }
11
12
    match(name) {
0 ignored issues
show
Unused Code introduced by
The parameter name is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
13
        return false;
14
    }
15
16
    call(cmd) {
17
        this.api.loading.show();
18
        this.makeRequest(cmd.command).then((response) => {
19
            let responseResult = response.result ? response.result : response;
20
            this.api.loading.hide();
21
            this.api.echo(responseResult);
22
            this.api.serverInfo();
23
        }, (response) => {
24
            let responseResult = response.result ? response.result : response;
25
            this.api.loading.hide();
26
            this.api.echo(responseResult);
27
            this.api.serverInfo();
28
        });
29
    }
30
31
    addslashes(str) {
32
        return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
33
    }
34
35
    makeRequest(command) {
36
        return new Promise((resolve, reject) => {
37
            $.ajax({
38
                url: this.api.options.endpoint,
39
                dataType: 'json',
40
                type: 'post',
41
                headers: {
42
                    'X-CSRF-TOKEN': this.api.options.csrfToken
43
                },
44
                data: {
45
                    jsonrpc: '2.0',
46
                    id: ++this.requestId,
47
                    command: command
48
                },
49
                success: (response) => {
50
                    if (response.error !== 0) {
51
                        reject(response);
52
                        return;
53
                    }
54
55
                    resolve(response);
56
                },
57
                error: (jqXhr, json, errorThrown) => {
58
                    reject({
59
                        result: `${jqXhr.status}: ${errorThrown}`
60
                    });
61
                }
62
            });
63
        });
64
    }
65
}
66