Passed
Push — feature/code-analysis ( e964aa...4fe35d )
by Jonathan
14:33
created

AdminConfigAction::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 31
nc 3
nop 1
dl 0
loc 52
rs 9.424
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-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Certificates
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\Certificates\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\Auth;
18
use Fisharebest\Webtrees\FlashMessages;
19
use Fisharebest\Webtrees\I18N;
20
use Fisharebest\Webtrees\Tree;
21
use Fisharebest\Webtrees\Validator;
22
use Fisharebest\Webtrees\Services\ModuleService;
23
use MyArtJaub\Webtrees\Module\Certificates\CertificatesModule;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
use Psr\Http\Server\RequestHandlerInterface;
27
28
/**
29
 * Request handler for saving configuration of the module.
30
 */
31
class AdminConfigAction implements RequestHandlerInterface
32
{
33
    private ?CertificatesModule $module;
34
35
    /**
36
     * Constructor for Admin Config Action request handler
37
     *
38
     * @param ModuleService $module_service
39
     */
40
    public function __construct(ModuleService $module_service)
41
    {
42
        $this->module = $module_service->findByInterface(CertificatesModule::class)->first();
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
48
     */
49
    public function handle(ServerRequestInterface $request): ResponseInterface
50
    {
51
        $tree = Validator::attributes($request)->tree();
52
53
        $admin_config_route = route(AdminConfigPage::class, ['tree' => $tree->name()]);
54
55
        if ($this->module === null) {
56
            FlashMessages::addMessage(
57
                I18N::translate('The attached module could not be found.'),
58
                'danger'
59
            );
60
            return redirect($admin_config_route);
61
        }
62
63
        $tree->setPreference(
64
            'MAJ_CERTIF_SHOW_CERT',
65
            (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE)
66
        );
67
        $tree->setPreference(
68
            'MAJ_CERTIF_SHOW_NO_WATERMARK',
69
            (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE)
70
        );
71
        $tree->setPreference(
72
            'MAJ_CERTIF_WM_DEFAULT',
73
            Validator::parsedBody($request)->string('MAJ_CERTIF_WM_DEFAULT', '')
74
        );
75
76
        $tree->setPreference(
77
            'MAJ_CERTIF_WM_FONT_MAXSIZE',
78
            (string) (
79
                Validator::parsedBody($request)->isBetween(0, PHP_INT_MAX)->integer('MAJ_CERTIF_WM_FONT_MAXSIZE', 18)
80
            )
81
        );
82
83
        // Only accept valid color for MAJ_WM_FONT_COLOR
84
        $watermark_color = Validator::parsedBody($request)->string('MAJ_CERTIF_WM_FONT_COLOR', '');
85
        if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) {
86
            $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color);
87
        }
88
89
        // Only accept valid folders for MAJ_CERT_ROOTDIR
90
        $cert_root_dir = Validator::parsedBody($request)->string('MAJ_CERTIF_ROOTDIR', '');
91
        $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir) ?? '';
92
        $cert_root_dir = trim($cert_root_dir, '/') . '/';
93
        $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir);
94
95
        FlashMessages::addMessage(
96
            I18N::translate('The preferences for the module ā€œ%sā€ have been updated.', $this->module->title()),
97
            'success'
98
        );
99
100
        return redirect($admin_config_route);
101
    }
102
}
103