Completed
Push — main ( d77b46...305f9c )
by Bjarn
17s queued 11s
created

MemcacheController.execute   B

Complexity

Conditions 5

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
dl 0
loc 24
rs 8.9332
c 0
b 0
f 0
1
import Memcached from '../extensions/php/memcached'
2
import Memcache from '../services/memcache'
3
import {client} from '../utils/os'
4
import {getLinkedPhpVersion} from '../utils/phpFpm'
5
6
class MemcacheController {
7
8
    /**
9
     * Switch the service to the given version.
10
     */
11
    execute = async (status: string): Promise<boolean> => {
12
        if (status !== 'on' && status !== 'off') {
13
            console.log('Invalid status. Please provide status \'on\' or \'off\'.')
14
            return false
15
        }
16
17
        const memcache = new Memcache()
18
        const phpMemcached = new Memcached()
19
        let restart = false
20
21
        if (status === 'on') {
22
            restart = await this.enable(memcache, phpMemcached)
23
        }
24
25
        if (status === 'off') {
26
            restart = await this.disable(memcache, phpMemcached)
27
        }
28
29
        if (restart) {
30
            const php = await getLinkedPhpVersion()
31
            await php.restart()
32
        }
33
34
        return true
35
    }
36
37
    enable = async (memcache: Memcache, phpMemcached: Memcached): Promise<boolean> => {
38
        let restart = false
39
40
        if (await client().packageManager.packageIsInstalled(memcache.service)) {
41
            console.log(`${memcache.service} is already installed.`)
42
        } else {
43
            restart = true
44
            console.log(`Installing ${memcache.service}...`)
45
            await memcache.install()
46
            console.log(`${memcache.service} has been installed`)
47
        }
48
49
        console.log('Install Memcached PHP extension...')
50
51
        // Memcache is ready, now install the PHP extension.
52
        const phpExtensionInstalled = await phpMemcached.install()
53
54
        return restart || phpExtensionInstalled
55
    }
56
57
    disable = async (memcache: Memcache, phpMemcached: Memcached): Promise<boolean> => {
58
        const phpExtensionDisabled = await phpMemcached.disable()
59
60
        if (phpExtensionDisabled) {
61
            console.log('Disabled memcache\'s PHP extension')
62
        } else {
63
            console.log('Memcache\'s PHP extension was not enabled.')
64
        }
65
66
        if (!(await client().packageManager.packageIsInstalled(memcache.service))) {
67
            console.log(`${memcache.service} was not installed.`)
68
            return phpExtensionDisabled
69
        }
70
71
        console.log(`Uninstalling ${memcache.service}...`)
72
        await memcache.uninstall()
73
        console.log(`${memcache.service} has been uninstalled`)
74
75
        return true
76
    }
77
78
}
79
80
export default MemcacheController