Passed
Push — develop ( eeeda6...0194c7 )
by Jens
03:21
created

FilesStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 19.79 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 19
loc 96
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFiles() 0 6 1
B addFile() 0 25 3
A getFileByName() 0 11 3
A deleteFileByName() 19 19 4
A compareFiles() 0 4 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
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace library\storage\storage;
7
8
9
class FilesStorage extends Storage
10
{
11
	/**
12
	 * @return array
13
	 */
14
	public function getFiles() {
15
		$files = $this->repository->files;
16
		usort($files, array($this, 'compareFiles'));
17
18
		return $files;
19
	}
20
21
	/**
22
	 * @param $postValues
23
	 *
24
	 * @throws \Exception
25
	 */
26
	public function addFile($postValues)
27
	{
28
		$destinationPath = realpath(__DIR__ . '/../../../www/files/');
29
30
		$filename = $this->validateFilename($postValues['name'], $destinationPath);
31
		$destination = $destinationPath . '/' . $filename;
32
33
		if ($postValues['error'] != '0') {
34
			throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
35
		}
36
37
		if (move_uploaded_file($postValues['tmp_name'], $destination)) {
38
			$file = new \stdClass();
39
			$file->file = $filename;
40
			$file->type = $postValues['type'];
41
			$file->size = $postValues['size'];
42
43
			$files = $this->repository->files;
44
			$files[] = $file;
45
			$this->repository->files = $files;
46
			$this->save();
47
		} else {
48
			throw new \Exception('Error moving uploaded file');
49
		}
50
	}
51
52
	/**
53
	 * @param $filename
54
	 *
55
	 * @return null
56
	 */
57
	public function getFileByName($filename)
58
	{
59
		$files = $this->getFiles();
60
		foreach ($files as $file) {
61
			if ($filename == $file->file) {
62
				return $file;
63
			}
64
		}
65
66
		return null;
67
	}
68
69
	/**
70
	 * @param $filename
71
	 *
72
	 * @throws \Exception
73
	 */
74 View Code Duplication
	public function deleteFileByName($filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
	{
76
		$destinationPath = realpath(__DIR__ . '/../../../www/files/');
77
		$destination = $destinationPath . '/' . $filename;
78
79
		if (file_exists($destination)) {
80
			$files = $this->getFiles();
81
			foreach ($files as $key => $file) {
82
				if ($file->file == $filename) {
83
					unlink($destination);
84
					unset($files[$key]);
85
				}
86
			}
87
88
			$files = array_values($files);
89
			$this->repository->files = $files;
90
			$this->save();
91
		}
92
	}
93
94
	/**
95
	 * @param $a
96
	 * @param $b
97
	 *
98
	 * @return int
99
	 */
100
	private function compareFiles($a, $b)
101
	{
102
		return strcmp($a->file, $b->file);
103
	}
104
}