ApplicationUtils::checkExtensionRequirements()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebThumbnailer\Utils;
6
7
use WebThumbnailer\Exception\MissingRequirementException;
8
9
class ApplicationUtils
10
{
11
    /**
12
     * @param string[] $required list of required extension names
13
     *
14
     * @return bool True if the check is successful.
15
     *
16
     * @throws MissingRequirementException
17
     */
18
    public static function checkExtensionRequirements(array $required): bool
19
    {
20
        foreach ($required as $extension) {
21
            if (! extension_loaded($extension)) {
22
                throw new MissingRequirementException(sprintf(
23
                    'PHP extension php-%s is required and must be loaded',
24
                    $extension
25
                ));
26
            }
27
        }
28
29
        return true;
30
    }
31
32
    /**
33
     * Checks the PHP version to ensure WT can run
34
     *
35
     * @param string $minVersion minimum PHP required version
36
     * @param string $curVersion current PHP version (use PHP_VERSION)
37
     *
38
     * @return bool True if the check is successful.
39
     *
40
     * @throws MissingRequirementException
41
     */
42
    public static function checkPHPVersion(string $minVersion, string $curVersion): bool
43
    {
44
        if (version_compare($curVersion, $minVersion) < 0) {
45
            throw new MissingRequirementException(sprintf(
46
                'Your PHP version is obsolete! Expected at least %s, found %s.',
47
                $minVersion,
48
                $curVersion
49
            ));
50
        }
51
52
        return true;
53
    }
54
}
55