xref: /webtrees/app/Http/RequestHandlers/DataFixSelect.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify
7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by
8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ce42304aSGreg Roach * (at your option) any later version.
10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful,
11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ce42304aSGreg Roach * GNU General Public License for more details.
14ce42304aSGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ce42304aSGreg Roach
22ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
24ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree;
25ce42304aSGreg Roachuse Psr\Http\Message\ResponseInterface;
26ce42304aSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27ce42304aSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28ce42304aSGreg Roach
29ce42304aSGreg Roachuse function assert;
30ce42304aSGreg Roachuse function redirect;
31ce42304aSGreg Roachuse function route;
32ce42304aSGreg Roach
33ce42304aSGreg Roach/**
34ce42304aSGreg Roach * Run a data-fix.
35ce42304aSGreg Roach */
36ce42304aSGreg Roachclass DataFixSelect implements RequestHandlerInterface
37ce42304aSGreg Roach{
38ce42304aSGreg Roach    /** @var ModuleService */
39ce42304aSGreg Roach    private $module_service;
40ce42304aSGreg Roach
41ce42304aSGreg Roach    /**
42ce42304aSGreg Roach     * DataFix constructor.
43ce42304aSGreg Roach     *
44ce42304aSGreg Roach     * @param ModuleService $module_service
45ce42304aSGreg Roach     */
46ce42304aSGreg Roach    public function __construct(ModuleService $module_service)
47ce42304aSGreg Roach    {
48ce42304aSGreg Roach        $this->module_service = $module_service;
49ce42304aSGreg Roach    }
50ce42304aSGreg Roach
51ce42304aSGreg Roach    /**
52ce42304aSGreg Roach     * @param ServerRequestInterface $request
53ce42304aSGreg Roach     *
54ce42304aSGreg Roach     * @return ResponseInterface
55ce42304aSGreg Roach     */
56ce42304aSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
57ce42304aSGreg Roach    {
58ce42304aSGreg Roach        $tree = $request->getAttribute('tree');
59ce42304aSGreg Roach        assert($tree instanceof Tree);
60ce42304aSGreg Roach
61ce42304aSGreg Roach        $data_fixes = $this->module_service->findByInterface(ModuleDataFixInterface::class);
62ce42304aSGreg Roach        $data_fix   = $request->getParsedBody()['data_fix'] ?? '';
63ce42304aSGreg Roach
64ce42304aSGreg Roach        $module = $data_fixes->get($data_fix);
65ce42304aSGreg Roach
66ce42304aSGreg Roach        if ($module instanceof ModuleDataFixInterface) {
67ce42304aSGreg Roach            return redirect(route(DataFixPage::class, ['tree' => $tree->name(), 'data_fix' => $module->name()]));
68ce42304aSGreg Roach        }
69ce42304aSGreg Roach
70ce42304aSGreg Roach        return redirect(route(DataFixPage::class, ['tree' => $tree->name()]));
71ce42304aSGreg Roach    }
72ce42304aSGreg Roach}
73