Passed
Branch — main (f9aaf7)
by Jonathan
14:43
created

ModulesHooksPage   A

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