EmailPreferencesPage::handle()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 56
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 44
nc 2
nop 1
dl 0
loc 56
rs 8.2826
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Services\EmailService;
25
use Fisharebest\Webtrees\Site;
26
use Fisharebest\Webtrees\Webtrees;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Webtrees was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function filter_var;
32
use function gethostbyaddr;
33
use function gethostbyname;
34
use function gethostname;
35
36
use const FILTER_VALIDATE_DOMAIN;
37
38
/**
39
 * Edit the email preferences.
40
 */
41
class EmailPreferencesPage implements RequestHandlerInterface
42
{
43
    use ViewResponseTrait;
44
45
    private EmailService $email_service;
46
47
    /**
48
     * @param EmailService $email_service
49
     */
50
    public function __construct(EmailService $email_service)
51
    {
52
        $this->email_service = $email_service;
53
    }
54
55
    /**
56
     * @param ServerRequestInterface $request
57
     *
58
     * @return ResponseInterface
59
     */
60
    public function handle(ServerRequestInterface $request): ResponseInterface
61
    {
62
        $mail_ssl_options       = $this->email_service->mailSslOptions();
63
        $mail_transport_options = $this->email_service->mailTransportOptions();
64
65
        $title = I18N::translate('Sending email');
66
67
        $SMTP_ACTIVE    = Site::getPreference('SMTP_ACTIVE');
68
        $SMTP_AUTH      = Site::getPreference('SMTP_AUTH');
69
        $SMTP_AUTH_USER = Site::getPreference('SMTP_AUTH_USER');
70
        $SMTP_DISP_NAME = Site::getPreference('SMTP_DISP_NAME');
71
        $SMTP_FROM_NAME = Site::getPreference('SMTP_FROM_NAME');
72
        $SMTP_HELO      = Site::getPreference('SMTP_HELO');
73
        $SMTP_HOST      = Site::getPreference('SMTP_HOST');
74
        $SMTP_PORT      = Site::getPreference('SMTP_PORT');
75
        $SMTP_SSL       = Site::getPreference('SMTP_SSL');
76
        $DKIM_DOMAIN    = Site::getPreference('DKIM_DOMAIN');
77
        $DKIM_SELECTOR  = Site::getPreference('DKIM_SELECTOR');
78
        $DKIM_KEY       = Site::getPreference('DKIM_KEY');
79
80
        $hostname = gethostname() ?: 'localhost';
81
        $ip       = gethostbyname($hostname);
82
83
        if ($ip !== $hostname) {
84
            $hostname = gethostbyaddr($ip);
85
        }
86
87
        // Defaults
88
        $SMTP_PORT      = $SMTP_PORT ?: '25';
89
        $SMTP_HELO      = $SMTP_HELO ?: $hostname;
90
        $SMTP_FROM_NAME = $SMTP_FROM_NAME ?: ('no-reply@' . $SMTP_HELO);
91
        $SMTP_DISP_NAME = $SMTP_DISP_NAME ?: Webtrees::NAME;
92
93
        $smtp_from_name_valid = $this->email_service->isValidEmail($SMTP_FROM_NAME);
94
        $smtp_helo_valid      = filter_var($SMTP_HELO, FILTER_VALIDATE_DOMAIN);
95
96
        $this->layout = 'layouts/administration';
97
98
        return $this->viewResponse('admin/site-mail', [
99
            'mail_ssl_options'       => $mail_ssl_options,
100
            'mail_transport_options' => $mail_transport_options,
101
            'title'                  => $title,
102
            'smtp_helo_valid'        => $smtp_helo_valid,
103
            'smtp_from_name_valid'   => $smtp_from_name_valid,
104
            'SMTP_ACTIVE'            => $SMTP_ACTIVE,
105
            'SMTP_AUTH'              => $SMTP_AUTH,
106
            'SMTP_AUTH_USER'         => $SMTP_AUTH_USER,
107
            'SMTP_DISP_NAME'         => $SMTP_DISP_NAME,
108
            'SMTP_FROM_NAME'         => $SMTP_FROM_NAME,
109
            'SMTP_HELO'              => $SMTP_HELO,
110
            'SMTP_HOST'              => $SMTP_HOST,
111
            'SMTP_PORT'              => $SMTP_PORT,
112
            'SMTP_SSL'               => $SMTP_SSL,
113
            'DKIM_DOMAIN'            => $DKIM_DOMAIN,
114
            'DKIM_SELECTOR'          => $DKIM_SELECTOR,
115
            'DKIM_KEY'               => $DKIM_KEY,
116
        ]);
117
    }
118
}
119