Passed
Push — 3.0 ( 5e1ed3...5a5495 )
by Rubén
04:22
created

UpgradeAppService::upgrade()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 2
dl 0
loc 28
rs 9.7333
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\ConfigData;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\Providers\Log\FileLogHandler;
31
use SP\Services\Service;
32
use SP\Util\VersionUtil;
33
34
/**
35
 * Class UpgradeAppService
36
 *
37
 * @package SP\Services\Upgrade
38
 */
39
final class UpgradeAppService extends Service implements UpgradeInterface
40
{
41
    const UPGRADES = ['300.18010101', '300.18072901', '300.18072902'];
42
43
    /**
44
     * @param $version
45
     *
46
     * @return bool
47
     */
48
    public static function needsUpgrade($version)
49
    {
50
        return VersionUtil::checkVersion($version, self::UPGRADES);
51
    }
52
53
    /**
54
     * @param            $version
55
     * @param ConfigData $configData
56
     *
57
     * @throws UpgradeException
58
     * @throws \DI\DependencyException
59
     * @throws \DI\NotFoundException
60
     * @throws \SP\Storage\File\FileException
61
     */
62
    public function upgrade($version, ConfigData $configData)
63
    {
64
        $this->eventDispatcher->notifyEvent('upgrade.app.start',
65
            new Event($this, EventMessage::factory()
66
                ->addDescription(__u('Update Application')))
67
        );
68
69
        foreach (self::UPGRADES as $appVersion) {
70
            if (VersionUtil::checkVersion($version, $appVersion)) {
71
                if ($this->applyUpgrade($appVersion) === false) {
72
                    throw new UpgradeException(
73
                        __u('Error while applying the application update'),
74
                        UpgradeException::CRITICAL,
75
                        __u('Please, check the event log for more details')
76
                    );
77
                }
78
79
                logger('APP Upgrade: ' . $appVersion);
80
81
                $configData->setConfigVersion($appVersion);
82
83
                $this->config->saveConfig($configData, false);
84
            }
85
        }
86
87
        $this->eventDispatcher->notifyEvent('upgrade.app.end',
88
            new Event($this, EventMessage::factory()
89
                ->addDescription(__u('Update Application')))
90
        );
91
    }
92
93
    /**
94
     * Actualizaciones de la aplicación
95
     *
96
     * @param $version
97
     *
98
     * @return bool
99
     */
100
    private function applyUpgrade($version)
101
    {
102
        try {
103
            switch ($version) {
104
                case '300.18010101':
105
                    $this->dic->get(UpgradeCustomFieldDefinition::class)
106
                        ->upgrade_300_18010101();
107
                    $this->dic->get(UpgradePublicLink::class)
108
                        ->upgrade_300_18010101();
109
                    return true;
110
                case '300.18072901':
111
                    $this->dic->get(UpgradeCustomFieldDefinition::class)
112
                        ->upgrade_300_18072901();
113
                    $this->dic->get(UpgradeAuthToken::class)
114
                        ->upgrade_300_18072901();
115
                    return true;
116
                case '300.18072902':
117
                    $this->dic->get(UpgradeCustomFieldData::class)
118
                        ->upgrade_300_18072902();
119
                    return true;
120
            }
121
        } catch (\Exception $e) {
122
            processException($e);
123
        }
124
125
        return false;
126
    }
127
128
    /**
129
     * initialize
130
     */
131
    protected function initialize()
132
    {
133
        $this->eventDispatcher->attach($this->dic->get(FileLogHandler::class));
134
    }
135
}