AdminConfigAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 50 3
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\Registry;
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
        if ($this->module === null) {
54
            FlashMessages::addMessage(
55
                I18N::translate('The attached module could not be found.'),
56
                'danger'
57
            );
58
            return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]);
59
        }
60
61
        $tree->setPreference(
62
            'MAJ_CERTIF_SHOW_CERT',
63
            (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_CERT', Auth::PRIV_HIDE)
64
        );
65
        $tree->setPreference(
66
            'MAJ_CERTIF_SHOW_NO_WATERMARK',
67
            (string) Validator::parsedBody($request)->integer('MAJ_CERTIF_SHOW_NO_WATERMARK', Auth::PRIV_HIDE)
68
        );
69
        $tree->setPreference(
70
            'MAJ_CERTIF_WM_DEFAULT',
71
            Validator::parsedBody($request)->string('MAJ_CERTIF_WM_DEFAULT', '')
72
        );
73
74
        $tree->setPreference(
75
            'MAJ_CERTIF_WM_FONT_MAXSIZE',
76
            (string) (
77
                Validator::parsedBody($request)->isBetween(0, PHP_INT_MAX)->integer('MAJ_CERTIF_WM_FONT_MAXSIZE', 18)
78
            )
79
        );
80
81
        // Only accept valid color for MAJ_WM_FONT_COLOR
82
        $watermark_color = Validator::parsedBody($request)->string('MAJ_CERTIF_WM_FONT_COLOR', '');
83
        if (preg_match('/#([a-fA-F0-9]{3}){1,2}/', $watermark_color) === 1) {
84
            $tree->setPreference('MAJ_CERTIF_WM_FONT_COLOR', $watermark_color);
85
        }
86
87
        // Only accept valid folders for MAJ_CERT_ROOTDIR
88
        $cert_root_dir = Validator::parsedBody($request)->string('MAJ_CERTIF_ROOTDIR', '');
89
        $cert_root_dir = preg_replace('/[:\/\\\\]+/', '/', $cert_root_dir) ?? '';
90
        $cert_root_dir = trim($cert_root_dir, '/') . '/';
91
        $tree->setPreference('MAJ_CERTIF_ROOTDIR', $cert_root_dir);
92
93
        FlashMessages::addMessage(
94
            I18N::translate('The preferences for the module ā€œ%sā€ have been updated.', $this->module->title()),
95
            'success'
96
        );
97
98
        return Registry::responseFactory()->redirect(AdminConfigPage::class, ['tree' => $tree->name()]);
99
    }
100
}
101