Passed
Push — develop ( 1d94a2...f31b27 )
by Jens
02:50
created

FilesRouting   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 15.52 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 58
rs 10
wmc 9
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 9 22 8
A downloadFile() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * User: jensk
4
 * Date: 30-1-2017
5
 * Time: 12:46
6
 */
7
8
namespace library\components\cms;
9
10
use library\cc\Request;
11
use library\components\CmsComponent;
12
13
class FilesRouting
14
{
15
	/**
16
	 * FilesRouting constructor.
17
	 *
18
	 * @param Request $request
19
	 * @param $relativeCmsUri
20
	 * @param CmsComponent $cmsComponent
21
	 */
22
	public function __construct($request, $relativeCmsUri, $cmsComponent)
23
	{
24
		if ($relativeCmsUri == '/files') {
25
			$cmsComponent->subTemplate = 'cms/files';
26
			$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES);
27
			$cmsComponent->setParameter(CmsComponent::PARAMETER_FILES, $cmsComponent->storage->getFiles());
28 View Code Duplication
		} elseif ($relativeCmsUri == '/files/new') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
			$cmsComponent->subTemplate = 'cms/files/form';
30
			$cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES);
31
			if (isset($_FILES[CmsComponent::FILES_PARAMETER_FILE])) {
32
				$cmsComponent->storage->addFile($_FILES[CmsComponent::FILES_PARAMETER_FILE]);
33
				header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files');
34
				exit;
35
			}
36
		} elseif ($relativeCmsUri == '/files/get' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) {
37
			$this->downloadFile($request::$get[CmsComponent::FILES_PARAMETER_FILE], $cmsComponent);
38
		} elseif ($relativeCmsUri == '/files/delete' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) {
39
			$cmsComponent->storage->deleteFileByName($request::$get[CmsComponent::FILES_PARAMETER_FILE]);
40
			header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files');
41
			exit;
42
		}
43
	}
44
45
	/**
46
	 * @param $slug
47
	 * @param $cmsComponent
48
	 */
49
	private function downloadFile($slug, $cmsComponent)
50
	{
51
		$file = $cmsComponent->storage->getFileByName($slug);
52
		$path = realpath(__DIR__ . '/../../../www/files/');
53
		$quoted = sprintf('"%s"', addcslashes(basename($path . '/' . $file->file), '"\\'));
54
		$size = filesize($path . '/' . $file->file);
55
56
		header('Content-Description: File Transfer');
57
		header('Content-Type: ' . $file->type);
58
		header('Content-Disposition: attachment; filename=' . $quoted);
59
		header('Content-Transfer-Encoding: binary');
60
		header('Connection: Keep-Alive');
61
		header('Expires: 0');
62
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
63
		header('Pragma: public');
64
		header('Content-Length: ' . $size);
65
66
		readfile($path . '/' . $file->file);
67
		exit;
68
	}
69
70
}