ModulesHooksAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

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