src/utils/phpFpm.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 76
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 63
mnd 4
bc 4
fnc 0
dl 0
loc 76
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import * as fs from 'fs'
2
import OS from '../client/OS'
3
import PhpFpm from '../services/phpFpm'
4
import PhpFpm72 from '../services/phpFpm72'
5
import PhpFpm73 from '../services/phpFpm73'
6
import PhpFpm74 from '../services/phpFpm74'
7
import PhpFpm80 from '../services/phpFpm80'
8
import PhpFpm81 from '../services/phpFpm81'
9
10
const supportedPhpVersions: string[] = [
11
    (new PhpFpm81).versionName,
12
    (new PhpFpm80).versionName,
13
    (new PhpFpm74).versionName,
14
    (new PhpFpm73).versionName,
15
    (new PhpFpm72).versionName,
16
]
17
18
const getPhpFpmByName = (phpVersion: string): PhpFpm => {
19
    let phpService: PhpFpm
20
21
    switch (phpVersion) {
22
    case (new PhpFpm72).service:
23
        phpService = new PhpFpm72()
24
        break
25
    case (new PhpFpm73).service:
26
        phpService = new PhpFpm73()
27
        break
28
    case (new PhpFpm74).service:
29
        phpService = new PhpFpm74()
30
        break
31
    case `${(new PhpFpm80).service}`:
32
        phpService = new PhpFpm80()
33
        break
34
    case `${(new PhpFpm81).service}@8.1`: // TODO: When PHP 8.2 is out, remove the hardcoded version.
35
        phpService = new PhpFpm81()
36
        break
37
    default:
38
        throw Error('Invalid PHP version: ' + phpVersion)
39
    }
40
41
    return phpService
42
}
43
44
/**
45
 * Get the currently linked Php Fpm binary.
46
 */
47
const getLinkedPhpVersion = async (): Promise<PhpFpm> => {
48
    const phpLink = await fs.lstatSync(`${OS.getInstance().usrLocalDir}/bin/php`)
49
50
    if (!phpLink.isSymbolicLink()) {
51
        throw Error('Php executable is not found.')
52
    }
53
54
    const phpBinary = await fs.realpathSync(`${OS.getInstance().usrLocalDir}/bin/php`)
55
56
    let linkedPhpVersion: PhpFpm | undefined
57
58
    supportedPhpVersions.forEach((versionName) => {
59
        if (phpBinary.includes(versionName)) {
60
            linkedPhpVersion = getPhpFpmByName(`php@${versionName}`)
61
        }
62
    })
63
64
    if (linkedPhpVersion) {
65
        return linkedPhpVersion
66
    } else {
67
        throw Error('Unable to determine linked PHP version')
68
    }
69
}
70
71
export {
72
    supportedPhpVersions,
73
    getPhpFpmByName,
74
    getLinkedPhpVersion
75
}
76