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

AbstractStorage::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
}