Conditions | 13 |
Paths | 24 |
Total Lines | 118 |
Code Lines | 66 |
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 |
||
246 | private function processFile(Request $request, ResourceNode $resourceNode, string $mode = 'show', string $filter = '') |
||
247 | { |
||
248 | $this->denyAccessUnlessGranted( |
||
249 | ResourceNodeVoter::VIEW, |
||
250 | $resourceNode, |
||
251 | $this->trans('Unauthorised view access to resource') |
||
252 | ); |
||
253 | |||
254 | $resourceFile = $resourceNode->getResourceFile(); |
||
255 | |||
256 | if (null === $resourceFile) { |
||
257 | throw new NotFoundHttpException($this->trans('File not found for resource')); |
||
258 | } |
||
259 | |||
260 | $fileName = $resourceNode->getResourceFile()->getOriginalName(); |
||
261 | $mimeType = $resourceFile->getMimeType(); |
||
262 | $resourceNodeRepo = $this->getResourceNodeRepository(); |
||
263 | |||
264 | switch ($mode) { |
||
265 | case 'download': |
||
266 | $forceDownload = true; |
||
267 | |||
268 | break; |
||
269 | case 'show': |
||
270 | default: |
||
271 | $forceDownload = false; |
||
272 | // If it's an image then send it to Glide. |
||
273 | if (str_contains($mimeType, 'image')) { |
||
274 | $glide = $this->getGlide(); |
||
275 | $server = $glide->getServer(); |
||
276 | $params = $request->query->all(); |
||
277 | |||
278 | // The filter overwrites the params from GET. |
||
279 | if (!empty($filter)) { |
||
280 | $params = $glide->getFilters()[$filter] ?? []; |
||
281 | } |
||
282 | |||
283 | // The image was cropped manually by the user, so we force to render this version, |
||
284 | // no matter other crop parameters. |
||
285 | $crop = $resourceFile->getCrop(); |
||
286 | if (!empty($crop)) { |
||
287 | $params['crop'] = $crop; |
||
288 | } |
||
289 | |||
290 | $fileName = $resourceNodeRepo->getFilename($resourceFile); |
||
291 | |||
292 | $response = $server->getImageResponse($fileName, $params); |
||
293 | |||
294 | $disposition = $response->headers->makeDisposition( |
||
295 | ResponseHeaderBag::DISPOSITION_INLINE, |
||
296 | basename($fileName) |
||
297 | ); |
||
298 | $response->headers->set('Content-Disposition', $disposition); |
||
299 | |||
300 | return $response; |
||
301 | } |
||
302 | |||
303 | // Modify the HTML content before displaying it. |
||
304 | if (str_contains($mimeType, 'html')) { |
||
305 | $content = $resourceNodeRepo->getResourceNodeFileContent($resourceNode); |
||
306 | |||
307 | $response = new Response(); |
||
308 | $disposition = $response->headers->makeDisposition( |
||
309 | ResponseHeaderBag::DISPOSITION_INLINE, |
||
310 | $fileName |
||
311 | ); |
||
312 | $response->headers->set('Content-Disposition', $disposition); |
||
313 | $response->headers->set('Content-Type', 'text/html'); |
||
314 | |||
315 | // @todo move into a function/class |
||
316 | if ('true' === $this->getSettingsManager()->getSetting('editor.translate_html')) { |
||
317 | $user = $this->getUser(); |
||
318 | if (null !== $user) { |
||
319 | // Overwrite user_json, otherwise it will be loaded by the TwigListener.php |
||
320 | $userJson = json_encode(['locale' => $user->getLocale()]); |
||
321 | $js = $this->renderView( |
||
322 | '@ChamiloCore/Layout/document.html.twig', |
||
323 | ['breadcrumb' => '', 'user_json' => $userJson] |
||
324 | ); |
||
325 | // Insert inside the head tag. |
||
326 | $content = str_replace('</head>', $js.'</head>', $content); |
||
327 | } |
||
328 | } |
||
329 | if ('true' === $this->getSettingsManager()->getSetting('course.enable_bootstrap_in_documents_html')) { |
||
330 | // It adds the bootstrap and awesome css |
||
331 | $links = '<link href="'.api_get_path(WEB_PATH).'libs/bootstrap/bootstrap.min.css" rel="stylesheet">'; |
||
332 | $links .= '<link href="'.api_get_path(WEB_PATH).'libs/bootstrap/font-awesome.min.css" rel="stylesheet">'; |
||
333 | // Insert inside the head tag. |
||
334 | $content = str_replace('</head>', $links.'</head>', $content); |
||
335 | } |
||
336 | $response->setContent($content); |
||
337 | /*$contents = $this->renderView('@ChamiloCore/Resource/view_html.twig', [ |
||
338 | 'category' => '...', |
||
339 | ]);*/ |
||
340 | |||
341 | return $response; |
||
342 | } |
||
343 | |||
344 | break; |
||
345 | } |
||
346 | |||
347 | $stream = $resourceNodeRepo->getResourceNodeFileStream($resourceNode); |
||
348 | |||
349 | $response = new StreamedResponse( |
||
350 | function () use ($stream): void { |
||
351 | stream_copy_to_stream($stream, fopen('php://output', 'wb')); |
||
352 | } |
||
353 | ); |
||
354 | |||
355 | //Transliterator::transliterate($fileName) |
||
356 | $disposition = $response->headers->makeDisposition( |
||
357 | $forceDownload ? ResponseHeaderBag::DISPOSITION_ATTACHMENT : ResponseHeaderBag::DISPOSITION_INLINE, |
||
358 | $fileName |
||
359 | ); |
||
360 | $response->headers->set('Content-Disposition', $disposition); |
||
361 | $response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream'); |
||
362 | |||
363 | return $response; |
||
364 | } |
||
366 |