Passed
Push — master ( c42747...1cfe16 )
by Greg
07:53
created

ReportGenerate::handle()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 38
c 1
b 0
f 0
nc 13
nop 1
dl 0
loc 62
rs 8.3786

How to fix   Long Method   

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) 2019 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 Fig\Http\Message\StatusCodeInterface;
23
use Fisharebest\Webtrees\Auth;
24
use Fisharebest\Webtrees\Contracts\UserInterface;
25
use Fisharebest\Webtrees\Http\ViewResponseTrait;
26
use Fisharebest\Webtrees\I18N;
27
use Fisharebest\Webtrees\Module\ModuleReportInterface;
28
use Fisharebest\Webtrees\Report\ReportHtml;
29
use Fisharebest\Webtrees\Report\ReportParserGenerate;
30
use Fisharebest\Webtrees\Report\ReportPdf;
31
use Fisharebest\Webtrees\Services\ModuleService;
32
use Fisharebest\Webtrees\Tree;
33
use InvalidArgumentException;
34
use Psr\Http\Message\ResponseInterface;
35
use Psr\Http\Message\ServerRequestInterface;
36
use Psr\Http\Server\RequestHandlerInterface;
37
38
use function addcslashes;
39
use function assert;
40
use function ob_get_clean;
41
use function ob_start;
42
use function redirect;
43
use function response;
44
use function route;
45
46
/**
47
 * Show all available reports.
48
 */
49
class ReportGenerate implements RequestHandlerInterface
50
{
51
    use ViewResponseTrait;
52
53
    /**
54
     * @var ModuleService
55
     */
56
    private $module_service;
57
58
    /**
59
     * ReportEngineController constructor.
60
     *
61
     * @param ModuleService $module_service
62
     */
63
    public function __construct(ModuleService $module_service)
64
    {
65
        $this->module_service = $module_service;
66
    }
67
68
    /**
69
     * A list of available reports.
70
     *
71
     * @param ServerRequestInterface $request
72
     *
73
     * @return ResponseInterface
74
     */
75
    public function handle(ServerRequestInterface $request): ResponseInterface
76
    {
77
        $tree = $request->getAttribute('tree');
78
        assert($tree instanceof Tree, new InvalidArgumentException());
79
80
        $user = $request->getAttribute('user');
81
        assert($user instanceof UserInterface, new InvalidArgumentException());
82
83
        $report = $request->getAttribute('report');
84
        $module = $this->module_service->findByName($report);
85
86
        if (!$module instanceof ModuleReportInterface) {
87
            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
88
        }
89
90
        Auth::checkComponentAccess($module, 'report', $tree, $user);
91
92
        $varnames  = $request->getQueryParams()['varnames'] ?? [];
93
        $vars      = $request->getQueryParams()['vars'] ?? [];
94
        $variables = [];
95
96
        foreach ($varnames as $name) {
97
            $variables[$name]['id'] = $vars[$name] ?? '';
98
        }
99
100
        $xml_filename = $module->resourcesFolder() . $module->xmlFilename();
101
102
        $format      = $request->getQueryParams()['format'] ?? '';
103
        $destination = $request->getQueryParams()['destination'] ?? '';
104
105
        switch ($format) {
106
            default:
107
            case 'HTML':
108
                ob_start();
109
                new ReportParserGenerate($xml_filename, new ReportHtml(), $variables, $tree);
110
                $html = ob_get_clean();
111
112
                $this->layout = 'layouts/report';
113
114
                $response = $this->viewResponse('report-page', [
115
                    'content' => $html,
116
                    'title'   => I18N::translate('Report'),
117
                ]);
118
119
                if ($destination === 'download') {
120
                    $response = $response->withHeader('Content-Disposition', 'attachment; filename="' . addcslashes($report, '"') . '.html"');
121
                }
122
123
                return $response;
124
125
            case 'PDF':
126
                ob_start();
127
                new ReportParserGenerate($xml_filename, new ReportPdf(), $variables, $tree);
128
                $pdf = ob_get_clean();
129
130
                $headers = ['Content-Type' => 'application/pdf'];
131
132
                if ($destination === 'download') {
133
                    $headers['Content-Disposition'] = 'attachment; filename="' . addcslashes($report, '"') . '.pdf"';
134
                }
135
136
                return response($pdf, StatusCodeInterface::STATUS_OK, $headers);
137
        }
138
    }
139
}
140