DataFixData::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\GedcomRecord;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\GedcomRecord was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
25
use Fisharebest\Webtrees\Services\DataFixService;
26
use Fisharebest\Webtrees\Services\DatatablesService;
27
use Fisharebest\Webtrees\Services\ModuleService;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\ModuleService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Fisharebest\Webtrees\Validator;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
33
use function assert;
34
use function e;
35
use function route;
36
use function view;
37
38
final class DataFixData implements RequestHandlerInterface
39
{
40
    private DataFixService $data_fix_service;
41
42
    private DatatablesService $datatables_service;
43
44
    private ModuleService $module_service;
45
46
    /**
47
     * @param DataFixService    $data_fix_service
48
     * @param DatatablesService $datatables_service
49
     * @param ModuleService     $module_service
50
     */
51
    public function __construct(
52
        DataFixService $data_fix_service,
53
        DatatablesService $datatables_service,
54
        ModuleService $module_service
55
    ) {
56
        $this->data_fix_service   = $data_fix_service;
57
        $this->module_service     = $module_service;
58
        $this->datatables_service = $datatables_service;
59
    }
60
61
    public function handle(ServerRequestInterface $request): ResponseInterface
62
    {
63
        $tree     = Validator::attributes($request)->tree();
64
        $data_fix = Validator::attributes($request)->string('data_fix', '');
65
        $module   = $this->module_service->findByName($data_fix);
66
        assert($module instanceof ModuleDataFixInterface);
67
68
        $params  = $request->getQueryParams();
69
        $records = $module->recordsToFix($tree, $params);
70
71
        $callback = function (object $row) use ($module, $params, $tree): array {
72
            $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type);
73
            assert($record instanceof GedcomRecord);
74
75
            $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
76
77
            if ($module->doesRecordNeedUpdate($record, $params)) {
78
                $preview_url = route(DataFixPreview::class, [
79
                        'tree'     => $tree->name(),
80
                        'data_fix' => $module->name(),
81
                        'action'   => 'update',
82
                        'xref'     => $row->xref,
83
                    ] + $params);
84
85
                $update_url = route(DataFixUpdate::class, [
86
                        'tree'     => $tree->name(),
87
                        'data_fix' => $module->name(),
88
                        'action'   => 'update',
89
                        'xref'     => $row->xref,
90
                    ] + $params);
91
92
                // wt-ajax-modal-title
93
                $col2 = '<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#wt-ajax-modal" data-wt-href="' . $preview_url . '">' . view('icons/search') . I18N::translate('Preview') . '</button>';
94
                $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>';
95
            } else {
96
                $col2 = '—';
97
            }
98
99
            return [$col1, $col2];
100
        };
101
102
        return $this->datatables_service->handleCollection($request, $records, [], [], $callback);
103
    }
104
}
105