ModulesHooksPage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Hooks
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2011-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\Hooks\Http\RequestHandlers;
16
17
use Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Validator;
19
use Fisharebest\Webtrees\Http\ViewResponseTrait;
20
use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
21
use MyArtJaub\Webtrees\Contracts\Hooks\HookInterface;
22
use MyArtJaub\Webtrees\Module\Hooks\Services\HookService;
23
use Psr\Http\Message\ResponseInterface;
24
use Psr\Http\Message\ServerRequestInterface;
25
use Psr\Http\Server\RequestHandlerInterface;
26
27
/**
28
 * Request handler for displaying the configuration of the modules implementing hooks
29
 */
30
class ModulesHooksPage implements RequestHandlerInterface
31
{
32
    use ViewResponseTrait;
33
34
    protected HookService $hook_service;
35
36
    /**
37
     * Constructor for ModulesHooksPage request handler
38
     *
39
     * @param HookService $hook_service
40
     */
41
    public function __construct(HookService $hook_service)
42
    {
43
        $this->hook_service = $hook_service;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
49
     */
50
    public function handle(ServerRequestInterface $request): ResponseInterface
51
    {
52
        $this->layout = 'layouts/administration';
53
54
        $hook_name = Validator::attributes($request)->string('hook_name', '');
55
        $hook = $this->hook_service->find($hook_name, true);
56
        if ($hook === null) {
57
            throw new HttpNotFoundException(I18N::translate('The hook with name ā€œ%sā€ does not exist.', $hook_name));
58
        }
59
60
        $modules = $hook->hooks()
61
            ->sortKeys()
62
            ->mapWithKeys(fn(HookInterface $hook) => [$hook->module()->name() => $hook->module()]);
63
64
        return $this->viewResponse('admin/components', [
65
            'description'    => $hook->description(),
66
            'modules'        => $modules,
67
            'title'          => $hook->title(),
68
            'uses_access'    => false,
69
            'uses_sorting'   => true
70
        ]);
71
    }
72
}
73