Passed
Push — master ( 6fd018...0b1b6f )
by Greg
06:09
created

ReportSetupPage::handle()   D

Complexity

Conditions 15
Paths 18

Size

Total Lines 137
Code Lines 99

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 99
c 2
b 0
f 0
nc 18
nop 1
dl 0
loc 137
rs 4.7551

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Auth;
23
use Fisharebest\Webtrees\Contracts\UserInterface;
24
use Fisharebest\Webtrees\Factory;
25
use Fisharebest\Webtrees\Html;
26
use Fisharebest\Webtrees\Http\ViewResponseTrait;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\Module\ModuleReportInterface;
29
use Fisharebest\Webtrees\Report\ReportParserSetup;
30
use Fisharebest\Webtrees\Services\LocalizationService;
31
use Fisharebest\Webtrees\Services\ModuleService;
32
use Fisharebest\Webtrees\Tree;
33
use Psr\Http\Message\ResponseInterface;
34
use Psr\Http\Message\ServerRequestInterface;
35
use Psr\Http\Server\RequestHandlerInterface;
36
37
use function assert;
38
use function redirect;
39
use function route;
40
41
/**
42
 * Get parameters for a report.
43
 */
44
class ReportSetupPage implements RequestHandlerInterface
45
{
46
    use ViewResponseTrait;
47
48
    /** @var LocalizationService */
49
    private $localization_service;
50
51
    /** @var ModuleService */
52
    private $module_service;
53
54
    /**
55
     * ReportEngineController constructor.
56
     *
57
     * @param LocalizationService $localization_service
58
     * @param ModuleService       $module_service
59
     */
60
    public function __construct(LocalizationService $localization_service, ModuleService $module_service)
61
    {
62
        $this->localization_service = $localization_service;
63
        $this->module_service = $module_service;
64
    }
65
66
    /**
67
     * @param ServerRequestInterface $request
68
     *
69
     * @return ResponseInterface
70
     */
71
    public function handle(ServerRequestInterface $request): ResponseInterface
72
    {
73
        $tree = $request->getAttribute('tree');
74
        assert($tree instanceof Tree);
75
76
        $user = $request->getAttribute('user');
77
        assert($user instanceof UserInterface);
78
79
        $report = $request->getAttribute('report');
80
        $module = $this->module_service->findByName($report);
81
82
        if (!$module instanceof ModuleReportInterface) {
83
            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
84
        }
85
86
        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
87
88
        $xref = $request->getQueryParams()['xref'] ?? '';
89
90
        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
91
92
        $report_array = (new ReportParserSetup($xml_filename))->reportProperties();
93
        $description  = $report_array['description'];
94
        $title        = $report_array['title'];
95
96
        $inputs = [];
97
98
        foreach ($report_array['inputs'] ?? [] as $n => $input) {
99
            $input += [
100
                'type'    => 'text',
101
                'default' => '',
102
                'lookup'  => '',
103
                'extra'   => '',
104
            ];
105
106
            $attributes = [
107
                'id'    => 'input-' . $n,
108
                'name'  => 'vars[' . $input['name'] . ']',
109
                'class' => $input['type'] === 'checkbox' ? 'form-control-check' : 'form-control',
110
            ];
111
112
            switch ($input['lookup']) {
113
                case 'INDI':
114
                    $input['control'] = view('components/select-individual', [
115
                        'id'         => 'input-' . $n,
116
                        'name'       => 'vars[' . $input['name'] . ']',
117
                        'individual' => Factory::individual()->make($xref, $tree),
118
                        'tree'       => $tree,
119
                        'required'   => true,
120
                    ]);
121
                    break;
122
123
                case 'FAM':
124
                    $input['control'] = view('components/select-family', [
125
                        'id'       => 'input-' . $n,
126
                        'name'     => 'vars[' . $input['name'] . ']',
127
                        'family'   => Factory::family()->make($xref, $tree),
128
                        'tree'     => $tree,
129
                        'required' => true,
130
                    ]);
131
                    break;
132
133
                case 'SOUR':
134
                    $input['control'] = view('components/select-source', [
135
                        'id'       => 'input-' . $n,
136
                        'name'     => 'vars[' . $input['name'] . ']',
137
                        'family'   => Factory::source()->make($xref, $tree),
138
                        'tree'     => $tree,
139
                        'required' => true,
140
                    ]);
141
                    break;
142
143
                case 'DATE':
144
                    // Need to know if the user prefers DMY/MDY/YMD so we can validate dates properly.
145
                    $dmy = $this->localization_service->dateFormatToOrder(I18N::dateFormat());
146
147
                    $attributes += [
148
                        'type'     => 'text',
149
                        'value'    => $input['default'],
150
                        'dir'      => 'ltr',
151
                        'onchange' => 'webtrees.reformatDate(this, "' . $dmy . '")'
152
                    ];
153
                    $input['control'] = '<input ' . Html::attributes($attributes) . '>';
154
                    $input['extra'] = view('edit/input-addon-calendar', ['id' => 'input-' . $n]);
155
                    break;
156
157
                default:
158
                    switch ($input['type']) {
159
                        case 'text':
160
                            $attributes += [
161
                                'type'  => 'text',
162
                                'value' => $input['default'],
163
                            ];
164
                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
165
                            break;
166
167
                        case 'checkbox':
168
                            $attributes += [
169
                                'type'    => 'checkbox',
170
                                'checked' => (bool) $input['default'],
171
                            ];
172
                            $input['control'] = '<input ' . Html::attributes($attributes) . '>';
173
                            break;
174
175
                        case 'select':
176
                            $options = [];
177
                            foreach (explode('|', $input['options']) as $option) {
178
                                [$key, $value] = explode('=>', $option);
179
                                if (preg_match('/^I18N::number\((.+?)(,([\d+]))?\)$/', $value, $match)) {
180
                                    $number        = (float) $match[1];
181
                                    $precision     = (int) ($match[3] ?? 0);
182
                                    $options[$key] = I18N::number($number, $precision);
183
                                } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) {
184
                                    $options[$key] = I18N::translate($match[1]);
185
                                } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) {
186
                                    $options[$key] = I18N::translateContext($match[1], $match[2]);
187
                                }
188
                            }
189
                            $input['control'] = view('components/select', ['name' => 'vars[' . $input['name'] . ']', 'id' => 'input-' . $n, 'selected' => $input['default'], 'options' => $options]);
190
                            break;
191
                    }
192
            }
193
194
            $inputs[] = $input;
195
        }
196
197
        $destination = $user->getPreference('default-report-destination', 'view');
198
        $format      = $user->getPreference('default-report-format', 'PDF');
199
200
        return $this->viewResponse('report-setup-page', [
201
            'description' => $description,
202
            'destination' => $destination,
203
            'format'      => $format,
204
            'inputs'      => $inputs,
205
            'report'      => $report,
206
            'title'       => $title,
207
            'tree'        => $tree,
208
        ]);
209
    }
210
}
211