Passed
Push — main ( b7799b...6c7a2f )
by Bjarn
03:09 queued 01:45
created

XdebugController.execute   B

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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