Conditions | 7 |
Paths | 13 |
Total Lines | 62 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
140 |