Passed
Push — main ( 98ebe5...c4cbcd )
by Greg
06:54
created

CheckForNewVersion   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 65
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 39 6
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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\Middleware;
21
22
use Fig\Http\Message\RequestMethodInterface;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Services\EmailService;
25
use Fisharebest\Webtrees\Services\UpgradeService;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Fisharebest\Webtrees\Site;
28
use Fisharebest\Webtrees\SiteUser;
29
use Fisharebest\Webtrees\Webtrees;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\MiddlewareInterface;
33
use Psr\Http\Server\RequestHandlerInterface;
34
use Throwable;
35
36
use function var_dump;
37
use function view;
38
39
/**
40
 * Middleware to check if a new version of webtrees is available.
41
 */
42
class CheckForNewVersion implements MiddlewareInterface
43
{
44
    private EmailService $email_service;
45
46
    private UpgradeService $upgrade_service;
47
48
    private UserService $user_service;
49
50
    /**
51
     * @param EmailService   $email_service
52
     * @param UpgradeService $upgrade_service
53
     * @param UserService    $user_service
54
     */
55
    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
56
    {
57
        $this->email_service   = $email_service;
58
        $this->upgrade_service = $upgrade_service;
59
        $this->user_service    = $user_service;
60
    }
61
62
    /**
63
     * @param ServerRequestInterface  $request
64
     * @param RequestHandlerInterface $handler
65
     *
66
     * @return ResponseInterface
67
     */
68
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
69
    {
70
        try {
71
            if ($request->getMethod() === RequestMethodInterface::METHOD_GET && $this->upgrade_service->isUpgradeAvailable()) {
72
                $latest_version       = $this->upgrade_service->latestVersion();
73
                $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
74
75
                // Have we emailed about this version before?
76
                if ($latest_version !== $latest_version_email) {
77
                    Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
78
79
                    // Yuck.  The email service needs a setting from config.ini.php - which is in the request.
80
                    Webtrees::set(ServerRequestInterface::class, $request);
81
82
                    foreach ($this->user_service->administrators() as $administrator) {
83
                        $this->email_service->send(
84
                            new SiteUser(),
85
                            $administrator,
86
                            new SiteUser(),
87
                            I18N::translate('A new version of webtrees is available.'),
88
                            view('emails/new-version-text', [
89
                                'latest_version' => $latest_version,
90
                                'recipient'      => $administrator,
91
                                'url'            => $request->getAttribute('base_url'),
92
                            ]),
93
                            view('emails/new-version-html', [
94
                                'latest_version' => $latest_version,
95
                                'recipient'      => $administrator,
96
                                'url'            => $request->getAttribute('base_url'),
97
                            ])
98
                        );
99
                    }
100
                }
101
            }
102
        } catch (Throwable $ex) {
103
            // We couldn't fetch the latest version or send an email? Nothing we can do...
104
        }
105
106
        return $handler->handle($request);
107
    }
108
}
109