Passed
Push — develop ( c22bc5...6872a8 )
by Jens
02:24
created

FilesStorage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 111
Duplicated Lines 17.12 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 19
loc 111
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
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
A getDestinationPath() 0 5 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 CloudControl\Cms\storage\storage;
7
8
9
class FilesStorage extends AbstractStorage
10
{
11
    protected $filesDir;
12
13
    public function __construct($repository, $filesDir)
14
    {
15
        parent::__construct($repository);
16
        $this->filesDir = $filesDir;
17
    }
18
19
20
    /**
21
	 * @return array
22
	 */
23
	public function getFiles() {
24
		$files = $this->repository->files;
25
		usort($files, array($this, 'compareFiles'));
26
27
		return $files;
28
	}
29
30
	/**
31
	 * @param $postValues
32
	 *
33
	 * @throws \Exception
34
	 */
35
	public function addFile($postValues)
36
	{
37
		$destinationPath = $this->getDestinationPath();
38
39
		$filename = $this->validateFilename($postValues['name'], $destinationPath);
40
		$destination = $destinationPath . '/' . $filename;
41
42
		if ($postValues['error'] != '0') {
43
			throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
44
		}
45
46
		if (move_uploaded_file($postValues['tmp_name'], $destination)) {
47
			$file = new \stdClass();
48
			$file->file = $filename;
49
			$file->type = $postValues['type'];
50
			$file->size = $postValues['size'];
51
52
			$files = $this->repository->files;
53
			$files[] = $file;
54
			$this->repository->files = $files;
55
			$this->save();
56
		} else {
57
			throw new \Exception('Error moving uploaded file');
58
		}
59
	}
60
61
	/**
62
	 * @param $filename
63
	 *
64
	 * @return null
65
	 */
66
	public function getFileByName($filename)
67
	{
68
		$files = $this->getFiles();
69
		foreach ($files as $file) {
70
			if ($filename == $file->file) {
71
				return $file;
72
			}
73
		}
74
75
		return null;
76
	}
77
78
	/**
79
	 * @param $filename
80
	 *
81
	 * @throws \Exception
82
	 */
83 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...
84
	{
85
		$destinationPath = $this->getDestinationPath();
86
		$destination = $destinationPath . '/' . $filename;
87
88
		if (file_exists($destination)) {
89
			$files = $this->getFiles();
90
			foreach ($files as $key => $file) {
91
				if ($file->file == $filename) {
92
					unlink($destination);
93
					unset($files[$key]);
94
				}
95
			}
96
97
			$files = array_values($files);
98
			$this->repository->files = $files;
99
			$this->save();
100
		}
101
	}
102
103
	/**
104
	 * @param $a
105
	 * @param $b
106
	 *
107
	 * @return int
108
	 */
109
	private function compareFiles($a, $b)
110
	{
111
		return strcmp($a->file, $b->file);
112
	}
113
114
    protected function getDestinationPath()
115
    {
116
        $destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR);
117
        return $destinationPath;
118
    }
119
}