Passed
Push — devel-3.0 ( 218a48...a00b1f )
by Rubén
03:54
created

Version   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersionArray() 0 11 2
A normalizeVersionForCompare() 0 15 4
B checkVersion() 0 24 8
A getVersionStringNormalized() 0 3 1
A getVersionArrayNormalized() 0 3 1
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 Version
31
 *
32
 * @package SP\Util
33
 */
34
class Version
35
{
36
37
    /**
38
     * Devolver versión normalizada en cadena
39
     *
40
     * @return string
41
     */
42
    public static function getVersionStringNormalized()
43
    {
44
        return implode('', Installer::VERSION) . '.' . Installer::BUILD;
45
    }
46
47
    /**
48
     * Comprobar si una versión necesita actualización
49
     *
50
     * @param string       $currentVersion
51
     * @param array|string $upgradeableVersion
52
     *
53
     * @return bool True si la versión es menor.
54
     */
55
    public static function checkVersion($currentVersion, $upgradeableVersion)
56
    {
57
        if (is_array($upgradeableVersion)) {
58
            $upgradeableVersion = $upgradeableVersion[count($upgradeableVersion) - 1];
59
        }
60
61
        $currentVersion = self::normalizeVersionForCompare($currentVersion);
62
        $upgradeableVersion = self::normalizeVersionForCompare($upgradeableVersion);
63
64
        if (empty($currentVersion) || empty($upgradeableVersion)) {
65
            return false;
66
        }
67
68
        if (PHP_INT_SIZE > 4) {
69
            return version_compare($currentVersion, $upgradeableVersion) === -1;
70
        }
71
72
        list($currentVersion, $build) = explode('.', $currentVersion, 2);
73
        list($upgradeVersion, $upgradeBuild) = explode('.', $upgradeableVersion, 2);
74
75
        $versionRes = (int)$currentVersion < (int)$upgradeVersion;
76
77
        return (($versionRes && (int)$upgradeBuild === 0)
78
            || ($versionRes && (int)$build < (int)$upgradeBuild));
79
    }
80
81
    /**
82
     * Devuelve una versión normalizada para poder ser comparada
83
     *
84
     * @param string $versionIn
85
     *
86
     * @return string
87
     */
88
    public static function normalizeVersionForCompare($versionIn)
89
    {
90
        if (is_string($versionIn) && !empty($versionIn)) {
91
            list($version, $build) = explode('.', $versionIn);
92
93
            $nomalizedVersion = 0;
94
95
            foreach (str_split($version) as $key => $value) {
96
                $nomalizedVersion += (int)$value * (10 ** (3 - $key));
97
            }
98
99
            return $nomalizedVersion . '.' . $build;
100
        }
101
102
        return '';
103
    }
104
105
    /**
106
     * Devuelve la versión de sysPass.
107
     *
108
     * @param bool $retBuild devolver el número de compilación
109
     *
110
     * @return array con el número de versión
111
     */
112
    public static function getVersionArray($retBuild = false)
113
    {
114
        $version = array_values(Installer::VERSION);
115
116
        if ($retBuild === true) {
117
            $version[] = Installer::BUILD;
118
119
            return $version;
120
        }
121
122
        return $version;
123
    }
124
125
    /**
126
     * Devolver versión normalizada en array
127
     *
128
     * @return array
129
     */
130
    public static function getVersionArrayNormalized()
131
    {
132
        return [implode('', Installer::VERSION), Installer::BUILD];
133
    }
134
}