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

ModulesHooksAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateHookOrder() 0 9 2
A handle() 0 18 3
A __construct() 0 4 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\FlashMessages;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Http\RequestHandlers\AbstractModuleComponentAction;
20
use Fisharebest\Webtrees\Services\ModuleService;
21
use Fisharebest\Webtrees\Services\TreeService;
22
use MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface;
23
use MyArtJaub\Webtrees\Module\Hooks\Services\HookService;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\ServerRequestInterface;
26
27
/**
28
 * Request handler for saving the configuration of the modules implementing hooks
29
 */
30
class ModulesHooksAction extends AbstractModuleComponentAction
31
{
32
    protected HookService $hook_service;
33
34
    /**
35
     * Constructor for ModulesHooksAction Request Handler
36
     *
37
     * @param ModuleService $module_service
38
     * @param TreeService $tree_service
39
     * @param HookService $hook_service
40
     */
41
    public function __construct(ModuleService $module_service, TreeService $tree_service, HookService $hook_service)
42
    {
43
        parent::__construct($module_service, $tree_service);
44
        $this->hook_service = $hook_service;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     * @see \Psr\Http\Server\RequestHandlerInterface::handle()
50
     */
51
    public function handle(ServerRequestInterface $request): ResponseInterface
52
    {
53
        $hook_name = $request->getAttribute('hook_name');
54
        $hook_collector = $this->hook_service->find($hook_name, true);
55
        if ($hook_collector === null) {
56
            FlashMessages::addMessage(I18N::translate('The hook with name ā€œ%sā€ does not exist.', $hook_name), 'danger');
57
            return redirect(AdminConfigPage::class);
58
        }
59
60
        foreach ($hook_collector->hooks() as $hook) {
61
            $this->updateStatus(get_class($hook->module()), $request);
62
        }
63
64
        $this->updateHookOrder($hook_collector, $request);
65
66
        FlashMessages::addMessage(I18N::translate('The hook preferences have been updated.'), 'success');
67
68
        return redirect(route(ModulesHooksPage::class, ['hook_name' => $hook_name]));
69
    }
70
71
    /**
72
     * Update the order of modules for a hook interface.
73
     *
74
     * @template THook of \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface
75
     * @param HookCollectorInterface<THook> $hook_collector
76
     * @param ServerRequestInterface $request
77
     */
78
    protected function updateHookOrder(HookCollectorInterface $hook_collector, ServerRequestInterface $request): void
79
    {
80
        $params = (array) $request->getParsedBody();
81
82
        $order = (array) ($params['order'] ?? []);
83
        $order = array_flip($order);
84
85
        foreach ($hook_collector->hooks() as $hook) {
86
            $this->hook_service->updateOrder($hook_collector, $hook->module(), $order[$hook->module()->name()] ?? 0);
87
        }
88
    }
89
}
90