Passed
Push — devel-3.0 ( 3ec0cb...3281db )
by Rubén
03:38
created

UpgradeUtil::checkAppVersion()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
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\Services\Upgrade;
26
27
use SP\Config\Config;
28
use SP\Util\PasswordUtil;
29
30
/**
31
 * Class UpgradeUtil
32
 *
33
 * @package SP\Services\Upgrade
34
 */
35
final class UpgradeUtil
36
{
37
    /**
38
     * Normalizar un número de versión
39
     *
40
     * @param $version
41
     *
42
     * @return string
43
     */
44
    public static function fixVersionNumber($version)
45
    {
46
        if (strpos($version, '.') === false) {
47
            if (strlen($version) === 10) {
48
                return substr($version, 0, 2) . '0.' . substr($version, 2);
49
            }
50
51
            return substr($version, 0, 3) . '.' . substr($version, 3);
52
        }
53
54
        return $version;
55
    }
56
57
    /**
58
     * Establecer la key de actualización
59
     *
60
     * @param Config $config
61
     *
62
     * @throws \DI\DependencyException
63
     * @throws \DI\NotFoundException
64
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
65
     * @throws \SP\Storage\File\FileException
66
     */
67
    public static function setUpgradeKey(Config $config)
68
    {
69
        $configData = $config->getConfigData();
70
        $upgradeKey = $configData->getUpgradeKey();
71
72
        if (empty($upgradeKey)) {
73
            $configData->setUpgradeKey(PasswordUtil::generateRandomBytes(32));
74
        }
75
76
        $configData->setMaintenance(true);
77
        $config->saveConfig($configData, false);
78
    }
79
}