src/controllers/xdebugController.ts   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 3.33

Size

Lines of Code 67
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 10
mnd 7
bc 7
fnc 3
bpm 2.3333
cpm 3.3333
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
B XdebugController.execute 0 23 5
A XdebugController.enable 0 12 2
A XdebugController.disable 0 14 3
1
import Xdebug from '../extensions/php/xdebug'
2
import {error, info, warning} from '../utils/console'
3
import {getLinkedPhpVersion} from '../utils/phpFpm'
4
5
class XdebugController {
6
7
    /**
8
     * Switch the service to the given version.
9
     */
10
    execute = async (status: string): Promise<boolean> => {
11
        if (status !== 'on' && status !== 'off') {
12
            error('Invalid status. Please provide status \'on\' or \'off\'.')
13
            return false
14
        }
15
16
        const xdebug = new Xdebug()
17
        let restart = false
18
19
        if (status === 'on') {
20
            restart = await this.enable(xdebug)
21
        }
22
23
        if (status === 'off') {
24
            restart = await this.disable(xdebug)
25
        }
26
27
        if (restart) {
28
            const php = await getLinkedPhpVersion()
29
            await php.restart()
30
        }
31
32
        return true
33
    }
34
35
    enable = async (xdebug: Xdebug): Promise<boolean> => {
36
        if (!(await xdebug.isInstalled())) {
37
            info('Extension xdebug is not installed. Installing now...')
38
            await xdebug.install()
39
        }
40
41
        // TODO: Enable auto start configuration for xdebug.
42
43
        info('Enabling xdebug...')
44
        await xdebug.enable()
45
46
        return true
47
    }
48
49
    disable = async (xdebug: Xdebug): Promise<boolean> => {
50
        if (!(await xdebug.isInstalled())) {
51
            warning('Extension xdebug is not installed. We do not need to disable it then...')
52
            return false
53
        }
54
55
        if (!(await xdebug.isEnabled())) {
56
            warning('Extension xdebug is not enabled.')
57
            return false
58
        }
59
60
        await xdebug.disable()
61
62
        return true
63
    }
64
65
}
66
67
export default XdebugController