Passed
Push — develop ( 7fbded...e50e9c )
by Greg
17:21 queued 05:10
created

CheckForNewVersion::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
rs 10
c 1
b 0
f 0
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 view;
37
38
/**
39
 * Middleware to check if a new version of webtrees is available.
40
 */
41
class CheckForNewVersion implements MiddlewareInterface
42
{
43
    private EmailService $email_service;
44
45
    private UpgradeService $upgrade_service;
46
47
    private UserService $user_service;
48
49
    /**
50
     * @param EmailService   $email_service
51
     * @param UpgradeService $upgrade_service
52
     * @param UserService    $user_service
53
     */
54
    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
55
    {
56
        $this->email_service   = $email_service;
57
        $this->upgrade_service = $upgrade_service;
58
        $this->user_service    = $user_service;
59
    }
60
61
    /**
62
     * @param ServerRequestInterface  $request
63
     * @param RequestHandlerInterface $handler
64
     *
65
     * @return ResponseInterface
66
     */
67
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
68
    {
69
        try {
70
            // Only run on full page requests.
71
            if (
72
                $request->getMethod() === RequestMethodInterface::METHOD_GET &&
73
                $request->getHeaderLine('X-Requested-With') !== '' &&
74
                $this->upgrade_service->isUpgradeAvailable()
75
            ) {
76
                $latest_version       = $this->upgrade_service->latestVersion();
77
                $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
78
79
                // Have we emailed about this version before?
80
                if ($latest_version !== $latest_version_email) {
81
                    Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
82
83
                    // Yuck.  The email service needs a setting from config.ini.php - which is in the request.
84
                    Webtrees::set(ServerRequestInterface::class, $request);
85
86
                    foreach ($this->user_service->administrators() as $administrator) {
87
                        $this->email_service->send(
88
                            new SiteUser(),
89
                            $administrator,
90
                            new SiteUser(),
91
                            I18N::translate('A new version of webtrees is available.'),
92
                            view('emails/new-version-text', [
93
                                'latest_version' => $latest_version,
94
                                'recipient'      => $administrator,
95
                                'url'            => $request->getAttribute('base_url'),
96
                            ]),
97
                            view('emails/new-version-html', [
98
                                'latest_version' => $latest_version,
99
                                'recipient'      => $administrator,
100
                                'url'            => $request->getAttribute('base_url'),
101
                            ])
102
                        );
103
                    }
104
                }
105
            }
106
        } catch (Throwable $ex) {
107
            // We couldn't fetch the latest version or send an email? Nothing we can do...
108
        }
109
110
        return $handler->handle($request);
111
    }
112
}
113