ReportingHelper::isVersionLess()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 19
c 1
b 0
f 0
nc 16
nop 2
dl 0
loc 34
rs 8.0555
1
<?php
2
3
namespace BiffBangPow\SSMonitor\Server\Helper;
4
5
class ReportingHelper
6
{
7
8
    /**
9
     * Compares Semver versions
10
     * Returns true is the version if lower than the threshold, false if the version is equal or greater
11
     * @param $version
12
     * @param $threshold
13
     * @return bool
14
     */
15
    public static function isVersionLess($version, $threshold)
16
    {
17
        $versionParts = explode('.', $version);
18
        $thresholdParts = explode('.', $threshold);
19
20
        // Compare major version numbers
21
        if ($versionParts[0] < $thresholdParts[0]) {
22
            return true;
23
        } elseif ($versionParts[0] > $thresholdParts[0]) {
24
            return false;
25
        }
26
27
        // Compare minor version numbers
28
        if ($versionParts[1] < $thresholdParts[1]) {
29
            return true;
30
        } elseif ($versionParts[1] > $thresholdParts[1]) {
31
            return false;
32
        }
33
34
        // Compare patch version numbers - default to zero if someone forgot to specify
35
        if (!isset($versionParts[2])) {
36
            $versionParts[2] = 0;
37
        }
38
        if (!isset($thresholdParts[2])) {
39
            $thresholdParts[2] = 0;
40
        }
41
        if ($versionParts[2] < $thresholdParts[2]) {
42
            return true;
43
        } elseif ($versionParts[2] > $thresholdParts[2]) {
44
            return false;
45
        }
46
47
        // Versions are equal or threshold is invalid
48
        return false;
49
    }
50
51
    /**
52
     * Extract the major version number for a given version string
53
     * @param $versionString
54
     * @return int|mixed|string|null
55
     */
56
    public static function getMajorVersion($versionString)
57
    {
58
        if (stristr($versionString, ".")) {
59
            $versionParts = explode(".", $versionString);
60
            return array_shift($versionParts);
61
        }
62
        else {
63
            return (int)$versionString;
64
        }
65
    }
66
67
68
    /**
69
     * Convert a memory string to bytes
70
     * @param $memoryString
71
     * @return int
72
     */
73
    public static function convertMemoryStringToBytes($memoryString)
74
    {
75
        $unit = strtoupper(substr($memoryString, -1));
76
        $size = (int)substr($memoryString, 0, -1);
77
78
        switch ($unit) {
79
            case 'K':
80
                $size *= 1024;
81
                break;
82
            case 'M':
83
                $size *= 1048576;
84
                break;
85
            case 'G':
86
                $size *= 1073741824;
87
                break;
88
            default:
89
                // Assume bytes if no matching unit is provided
90
                break;
91
        }
92
93
        return $size;
94
    }
95
96
}
97