Passed
Push — develop ( 39fefa...eeeda6 )
by Jens
03:13
created

ImagesStorage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 30
loc 105
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getImages() 0 4 1
B addImage() 0 26 3
B deleteImageByName() 23 23 5
A getImageByName() 0 11 3
A getImageSet() 7 7 2

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
use library\images\ImageResizer;
10
use library\storage\factories\ImageFactory;
11
12
class ImagesStorage extends Storage
13
{
14
	/**
15
	 * @var ImageSetStorage
16
	 */
17
	protected $imageSet;
18
19
	/**
20
	 * Get all images
21
	 *
22
	 * @return array
23
	 */
24
	public function getImages()
25
	{
26
		return $this->repository->images;
27
	}
28
29
	/**
30
	 * @param $postValues
31
	 *
32
	 * @throws \Exception
33
	 */
34
	public function addImage($postValues)
35
	{
36
		$destinationPath = realpath(__DIR__ . '/../../../www/images/');
37
38
		$filename = $this->validateFilename($postValues['name'], $destinationPath);
39
		$destination = $destinationPath . '/' . $filename;
40
41
		if ($postValues['error'] != '0') {
42
			throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
43
		}
44
45
		if (move_uploaded_file($postValues['tmp_name'], $destination)) {
46
			$imageResizer = new ImageResizer($this->getImageSet()->getImageSet());
47
			$fileNames = $imageResizer->applyImageSetToImage($destination);
48
			$fileNames['original'] = $filename;
49
			$imageObject = ImageFactory::createImageFromPostValues($postValues, $filename, $fileNames);
50
51
			$images = $this->repository->images;
52
			$images[] = $imageObject;
53
			$this->repository->images = $images;
54
55
			$this->save();
56
		} else {
57
			throw new \Exception('Error moving uploaded file');
58
		}
59
	}
60
61
	/**
62
	 * Delete image by name
63
	 * @param $filename
64
	 */
65 View Code Duplication
	public function deleteImageByName($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...
66
	{
67
		$destinationPath = realpath(__DIR__ . '/../../../www/images/');
68
69
		$images = $this->getImages();
70
71
		foreach ($images as $key => $image) {
72
			if ($image->file == $filename) {
73
				foreach ($image->set as $imageSetFilename) {
74
					$destination = $destinationPath . '/' . $imageSetFilename;
75
					if (file_exists($destination)) {
76
						unlink($destination);
77
					} else {
78
						dump($destination);
79
					}
80
				}
81
				unset($images[$key]);
82
			}
83
		}
84
85
		$this->repository->images = $images;
86
		$this->save();
87
	}
88
89
	/**
90
	 * @param $filename
91
	 *
92
	 * @return null
93
	 */
94
	public function getImageByName($filename)
95
	{
96
		$images = $this->getImages();
97
		foreach ($images as $image) {
98
			if ($image->file == $filename) {
99
				return $image;
100
			}
101
		}
102
103
		return null;
104
	}
105
106
	/**
107
	 * @return \library\storage\storage\ImageSetStorage
108
	 */
109 View Code Duplication
	private function getImageSet()
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...
110
	{
111
		if (!$this->imageSet instanceof ImageSetStorage) {
112
			$this->imageSet = new ImageSetStorage($this->repository);
113
		}
114
		return $this->imageSet;
115
	}
116
}