Passed
Push — develop ( 0cf906...cb779b )
by Jens
06:50
created

FilesStorage   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 15.97 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 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 getFilesDir() 0 4 1
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
     * @return mixed
105
     */
106
    public function getFilesDir()
107
    {
108
        return $this->filesDir;
109
    }
110
111
    /**
112
	 * @param $a
113
	 * @param $b
114
	 *
115
	 * @return int
116
	 */
117
	private function compareFiles($a, $b)
118
	{
119
		return strcmp($a->file, $b->file);
120
	}
121
122
    protected function getDestinationPath()
123
    {
124
        $destinationPath = realpath($this->filesDir . DIRECTORY_SEPARATOR);
125
        return $destinationPath;
126
    }
127
}