Passed
Push — develop ( 9c6499...a5e1bf )
by Jens
02:46
created

AbstractStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 35.48 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 22
loc 62
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validateFilename() 22 30 4
A save() 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 CloudControl\Cms\storage\storage;
7
8
9
use CloudControl\Cms\cc\StringUtil;
10
use CloudControl\Cms\storage\Repository;
11
12
abstract class AbstractStorage
13
{
14
	/**
15
	 * @var Repository
16
	 */
17
	protected $repository;
18
19
	public function __construct($repository)
20
	{
21
		$this->repository = $repository;
22
	}
23
24
	/**
25
	 * Converts filename to lowercase, remove non-ascii chars
26
	 * And adds "-copy" if the file already exists
27
	 *
28
	 * @param $filename
29
	 * @param $path
30
	 *
31
	 * @return string
32
	 */
33
	protected function validateFilename($filename, $path)
34
	{
35
		$fileParts = explode('.', $filename);
36 View Code Duplication
		if (count($fileParts) > 1) {
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...
37
			$extension = end($fileParts);
38
			array_pop($fileParts);
39
			$fileNameWithoutExtension = implode('-', $fileParts);
40
			$fileNameWithoutExtension = StringUtil::slugify($fileNameWithoutExtension);
41
			$filename = $fileNameWithoutExtension . '.' . $extension;
42
		} else {
43
			$filename = StringUtil::slugify($filename);
44
		}
45
46 View Code Duplication
		if (file_exists($path . '/' . $filename)) {
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...
47
			$fileParts = explode('.', $filename);
48
			if (count($fileParts) > 1) {
49
				$extension = end($fileParts);
50
				array_pop($fileParts);
51
				$fileNameWithoutExtension = implode('-', $fileParts);
52
				$fileNameWithoutExtension .= '-copy';
53
				$filename = $fileNameWithoutExtension . '.' . $extension;
54
			} else {
55
				$filename .= '-copy';
56
			}
57
58
			return $this->validateFilename($filename, $path);
59
		}
60
61
		return $filename;
62
	}
63
64
	/**
65
	 * Save changes made to the repository
66
	 *
67
	 * @throws \Exception
68
	 */
69
	protected function save()
70
	{
71
		$this->repository->save();
72
	}
73
}