Passed
Push — master ( 89f718...4b3ef6 )
by Greg
06:43
created

UpgradeWizardPage::wizardSteps()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 23
rs 9.7666
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Services\TreeService;
25
use Fisharebest\Webtrees\Services\UpgradeService;
26
use Fisharebest\Webtrees\Webtrees;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function basename;
32
use function e;
33
use function route;
34
use function version_compare;
35
36
/**
37
 * Upgrade to a new version of webtrees.
38
 */
39
class UpgradeWizardPage implements RequestHandlerInterface
40
{
41
    use ViewResponseTrait;
42
43
    // We make the upgrade in a number of small steps to keep within server time limits.
44
    private const STEP_CHECK    = 'Check';
45
    private const STEP_PREPARE  = 'Prepare';
46
    private const STEP_PENDING  = 'Pending';
47
    private const STEP_EXPORT   = 'Export';
48
    private const STEP_DOWNLOAD = 'Download';
49
    private const STEP_UNZIP    = 'Unzip';
50
    private const STEP_COPY     = 'Copy';
51
52
    /** @var UpgradeService */
53
    private $upgrade_service;
54
55
    /** @var TreeService */
56
    private $tree_service;
57
58
    /**
59
     * UpgradeController constructor.
60
     *
61
     * @param TreeService    $tree_service
62
     * @param UpgradeService $upgrade_service
63
     */
64
    public function __construct(TreeService $tree_service, UpgradeService $upgrade_service)
65
    {
66
        $this->tree_service    = $tree_service;
67
        $this->upgrade_service = $upgrade_service;
68
    }
69
70
    /**
71
     * @param ServerRequestInterface $request
72
     *
73
     * @return ResponseInterface
74
     */
75
    public function handle(ServerRequestInterface $request): ResponseInterface
76
    {
77
        $this->layout = 'layouts/administration';
78
79
        $continue = $request->getQueryParams()['continue'] ?? '';
80
81
        $title = I18N::translate('Upgrade wizard');
82
83
        $latest_version = $this->upgrade_service->latestVersion();
84
85
        $upgrade_available = version_compare($latest_version, Webtrees::VERSION) > 0;
86
87
        if ($upgrade_available && $continue === '1') {
88
            return $this->viewResponse('admin/upgrade/steps', [
89
                'steps' => $this->wizardSteps(),
90
                'title' => $title,
91
            ]);
92
        }
93
94
        return $this->viewResponse('admin/upgrade/wizard', [
95
            'current_version' => Webtrees::VERSION,
96
            'latest_version'  => $latest_version,
97
            'title'           => $title,
98
        ]);
99
    }
100
101
102
    /**
103
     * @return string[]
104
     */
105
    private function wizardSteps(): array
106
    {
107
        $download_url = $this->upgrade_service->downloadUrl();
108
109
        $export_steps = [];
110
111
        foreach ($this->tree_service->all() as $tree) {
112
            $route = route(UpgradeWizardStep::class, [
113
                'step' => self::STEP_EXPORT,
114
                'tree' => $tree->name(),
115
            ]);
116
117
            $export_steps[$route] = I18N::translate('Export all the family trees to GEDCOM files…') . ' ' . e($tree->title());
118
        }
119
120
        return [
121
                route(UpgradeWizardStep::class, ['step' => self::STEP_CHECK])   => I18N::translate('Upgrade wizard'),
122
                route(UpgradeWizardStep::class, ['step' => self::STEP_PREPARE]) => I18N::translate('Create a temporary folder…'),
123
                route(UpgradeWizardStep::class, ['step' => self::STEP_PENDING]) => I18N::translate('Check for pending changes…'),
124
            ] + $export_steps + [
125
                route(UpgradeWizardStep::class, ['step' => self::STEP_DOWNLOAD]) => I18N::translate('Download %s…', e($download_url)),
126
                route(UpgradeWizardStep::class, ['step' => self::STEP_UNZIP])    => I18N::translate('Unzip %s to a temporary folder…', e(basename($download_url))),
127
                route(UpgradeWizardStep::class, ['step' => self::STEP_COPY])     => I18N::translate('Copy files…'),
128
            ];
129
    }
130
}
131