Passed
Push — devel-3.0 ( 40e719...463a23 )
by Rubén
03:27
created

VersionUtil::checkVersion()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 14
nop 2
dl 0
loc 24
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Util;
26
27
use SP\Services\Install\Installer;
28
29
/**
30
 * Class VersionUtil
31
 *
32
 * @package SP\Util
33
 */
34
final class VersionUtil
35
{
36
    /**
37
     * Devolver versión normalizada en cadena
38
     *
39
     * @return string
40
     */
41
    public static function getVersionStringNormalized()
42
    {
43
        return implode('', Installer::VERSION) . '.' . Installer::BUILD;
44
    }
45
46
    /**
47
     * Comprobar si una versión necesita actualización
48
     *
49
     * @param string       $currentVersion
50
     * @param array|string $upgradeableVersion
51
     *
52
     * @return bool True si la versión es menor.
53
     */
54
    public static function checkVersion($currentVersion, $upgradeableVersion)
55
    {
56
        if (is_array($upgradeableVersion)) {
57
            $upgradeableVersion = $upgradeableVersion[count($upgradeableVersion) - 1];
58
        }
59
60
        $currentVersion = self::normalizeVersionForCompare($currentVersion);
61
        $upgradeableVersion = self::normalizeVersionForCompare($upgradeableVersion);
62
63
        if (empty($currentVersion) || empty($upgradeableVersion)) {
64
            return false;
65
        }
66
67
        if (PHP_INT_SIZE > 4) {
68
            return version_compare($currentVersion, $upgradeableVersion) === -1;
69
        }
70
71
        list($currentVersion, $build) = explode('.', $currentVersion, 2);
72
        list($upgradeVersion, $upgradeBuild) = explode('.', $upgradeableVersion, 2);
73
74
        $versionRes = (int)$currentVersion < (int)$upgradeVersion;
75
76
        return (($versionRes && (int)$upgradeBuild === 0)
77
            || ($versionRes && (int)$build < (int)$upgradeBuild));
78
    }
79
80
    /**
81
     * Devuelve una versión normalizada para poder ser comparada
82
     *
83
     * @param string $versionIn
84
     *
85
     * @return string
86
     */
87
    public static function normalizeVersionForCompare($versionIn)
88
    {
89
        if (is_string($versionIn) && !empty($versionIn)) {
90
            list($version, $build) = explode('.', $versionIn);
91
92
            $nomalizedVersion = 0;
93
94
            foreach (str_split($version) as $key => $value) {
95
                $nomalizedVersion += (int)$value * (10 ** (3 - $key));
96
            }
97
98
            return $nomalizedVersion . '.' . $build;
99
        }
100
101
        return '';
102
    }
103
104
    /**
105
     * Devuelve la versión de sysPass.
106
     *
107
     * @param bool $retBuild devolver el número de compilación
108
     *
109
     * @return array con el número de versión
110
     */
111
    public static function getVersionArray($retBuild = false)
112
    {
113
        $version = array_values(Installer::VERSION);
114
115
        if ($retBuild === true) {
116
            $version[] = Installer::BUILD;
117
118
            return $version;
119
        }
120
121
        return $version;
122
    }
123
124
    /**
125
     * Devolver versión normalizada en array
126
     *
127
     * @return array
128
     */
129
    public static function getVersionArrayNormalized()
130
    {
131
        return [implode('', Installer::VERSION), Installer::BUILD];
132
    }
133
}