Passed
Push — master ( 12c377...221005 )
by Rubén
04:12
created

UpgradeUtil::fixAppUpgrade()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 10
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\Config\ConfigData;
29
use SP\Util\PasswordUtil;
30
use SP\Util\VersionUtil;
31
32
/**
33
 * Class UpgradeUtil
34
 *
35
 * @package SP\Services\Upgrade
36
 */
37
final class UpgradeUtil
38
{
39
    /**
40
     * Normalizar un número de versión
41
     *
42
     * @param $version
43
     *
44
     * @return string
45
     */
46
    public static function fixVersionNumber($version)
47
    {
48
        if (strpos($version, '.') === false) {
49
            if (strlen($version) === 10) {
50
                return substr($version, 0, 2) . '0.' . substr($version, 2);
51
            }
52
53
            return substr($version, 0, 3) . '.' . substr($version, 3);
54
        }
55
56
        return $version;
57
    }
58
59
    /**
60
     * Establecer la key de actualización
61
     *
62
     * @param Config $config
63
     *
64
     * @throws \DI\DependencyException
65
     * @throws \DI\NotFoundException
66
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
67
     * @throws \SP\Storage\File\FileException
68
     */
69
    public static function setUpgradeKey(Config $config)
70
    {
71
        $configData = $config->getConfigData();
72
        $upgradeKey = $configData->getUpgradeKey();
73
74
        if (empty($upgradeKey)) {
75
            $configData->setUpgradeKey(PasswordUtil::generateRandomBytes(32));
76
        }
77
78
        $configData->setMaintenance(true);
79
        $config->saveConfig($configData, false);
80
    }
81
82
    /**
83
     * @param ConfigData $configData
84
     * @param Config $config
85
     * @throws \SP\Storage\File\FileException
86
     */
87
    public static function fixAppUpgrade(ConfigData $configData, Config $config)
88
    {
89
        // Fixes bug in 3.0.X version where some updates weren't applied
90
        // when upgrading from v2
91
        // $dbVersion is always '' when upgrading from v2
92
        if (!empty($configData->getDatabaseVersion())
93
            && empty($configData->getAppVersion())
94
        ) {
95
            $configData->setAppVersion(VersionUtil::getVersionStringNormalized());
96
            $config->saveConfig($configData, false);
97
        }
98
    }
99
}