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