Passed
Push — develop ( e50e9c...6c809f )
by Greg
20:25 queued 05:22
created

CheckForNewVersion::process()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 3
nop 2
dl 0
loc 32
rs 9.6
c 0
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\Module;
21
22
use Fisharebest\Webtrees\I18N;
23
use Fisharebest\Webtrees\Services\EmailService;
24
use Fisharebest\Webtrees\Services\UpgradeService;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Fisharebest\Webtrees\Site;
27
use Fisharebest\Webtrees\SiteUser;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\MiddlewareInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function view;
34
35
/**
36
 * Middleware to check if a new version of webtrees is available.
37
 */
38
class CheckForNewVersion extends AbstractModule implements MiddlewareInterface
39
{
40
    private EmailService $email_service;
41
42
    private UpgradeService $upgrade_service;
43
44
    private UserService $user_service;
45
46
    /**
47
     * @param EmailService   $email_service
48
     * @param UpgradeService $upgrade_service
49
     * @param UserService    $user_service
50
     */
51
    public function __construct(EmailService $email_service, UpgradeService $upgrade_service, UserService $user_service)
52
    {
53
        $this->email_service   = $email_service;
54
        $this->upgrade_service = $upgrade_service;
55
        $this->user_service    = $user_service;
56
    }
57
58
    /**
59
     * How should this module be identified in the control panel, etc.?
60
     *
61
     * @return string
62
     */
63
    public function title(): string
64
    {
65
        return I18N::translate('Check for new version');
66
    }
67
68
    /**
69
     * A sentence describing what this module does.
70
     *
71
     * @return string
72
     */
73
    public function description(): string
74
    {
75
        return I18N::translate('Send an email to all administrators when an upgrade is available.');
76
    }
77
78
    /**
79
     * @param ServerRequestInterface  $request
80
     * @param RequestHandlerInterface $handler
81
     *
82
     * @return ResponseInterface
83
     */
84
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
85
    {
86
        if ($this->upgrade_service->isUpgradeAvailable()) {
87
            $latest_version       = $this->upgrade_service->latestVersion();
88
            $latest_version_email = Site::getPreference('LATEST_WT_VERSION_EMAIL');
89
90
            // Have we emailed about this version before?
91
            if ($latest_version !== $latest_version_email) {
92
                Site::setPreference('LATEST_WT_VERSION_EMAIL', $latest_version);
93
94
                foreach ($this->user_service->administrators() as $administrator) {
95
                    $this->email_service->send(
96
                        new SiteUser(),
97
                        $administrator,
98
                        new SiteUser(),
99
                        I18N::translate('A new version of webtrees is available.'),
100
                        view('emails/new-version-text', [
101
                            'latest_version' => $latest_version,
102
                            'recipient'      => $administrator,
103
                            'url'            => $request->getAttribute('base_url'),
104
                        ]),
105
                        view('emails/new-version-html', [
106
                            'latest_version' => $latest_version,
107
                            'recipient'      => $administrator,
108
                            'url'            => $request->getAttribute('base_url'),
109
                        ])
110
                    );
111
                }
112
            }
113
        }
114
115
        return $handler->handle($request);
116
    }
117
}
118