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

Storage::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 17-3-2017.
4
 */
5
6
namespace library\storage\storage;
7
8
9
abstract class Storage
10
{
11
	/**
12
	 * @var Repository
13
	 */
14
	protected $repository;
15
16
	public function __construct($repository)
17
	{
18
		$this->repository = $repository;
19
	}
20
21
	/**
22
	 * Converts filename to lowercase, remove non-ascii chars
23
	 * And adds "-copy" if the file already exists
24
	 *
25
	 * @param $filename
26
	 * @param $path
27
	 *
28
	 * @return string
29
	 */
30
	protected function validateFilename($filename, $path)
31
	{
32
		$fileParts = explode('.', $filename);
33 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...
34
			$extension = end($fileParts);
35
			array_pop($fileParts);
36
			$fileNameWithoutExtension = implode('-', $fileParts);
37
			$fileNameWithoutExtension = slugify($fileNameWithoutExtension);
38
			$filename = $fileNameWithoutExtension . '.' . $extension;
39
		} else {
40
			$filename = slugify($filename);
41
		}
42
43 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...
44
			$fileParts = explode('.', $filename);
45
			if (count($fileParts) > 1) {
46
				$extension = end($fileParts);
47
				array_pop($fileParts);
48
				$fileNameWithoutExtension = implode('-', $fileParts);
49
				$fileNameWithoutExtension .= '-copy';
50
				$filename = $fileNameWithoutExtension . '.' . $extension;
51
			} else {
52
				$filename .= '-copy';
53
			}
54
55
			return $this->validateFilename($filename, $path);
56
		}
57
58
		return $filename;
59
	}
60
61
	/**
62
	 * Save changes made to the repository
63
	 *
64
	 * @throws \Exception
65
	 */
66
	protected function save()
67
	{
68
		$this->repository->save();
69
	}
70
}