PathManipulation   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A getReducedPath() 0 18 3
1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Olivier Paroz <[email protected]>
9
 *
10
 * @copyright Olivier Paroz 2017
11
 */
12
13
namespace OCA\Gallery\Controller;
14
15
/**
16
 * @package OCA\Gallery\Controller
17
 */
18
trait PathManipulation {
19
20
	/**
21
	 * Returns a shortened path for the gallery view
22
	 *
23
	 * We only want to keep one folder between the current folder and the found media file
24
	 * /root/folder/sub1/sub2/file.ext
25
	 * becomes
26
	 * /root/folder/file.ext
27
	 *
28
	 * @param string $path the full path to a file, which never starts with a slash
29
	 * @param string $currFolderPath the current folder, which never starts with a slash
30
	 *
31
	 * @return string
32
	 */
33
	private function getReducedPath($path, $currFolderPath) {
34
		// Adding a slash to make sure we don't cut a folder in half
35
		if ($currFolderPath) {
36
			$currFolderPath .= '/';
37
			$relativePath = str_replace($currFolderPath, '', $path);
38
		} else {
39
			$relativePath = $path;
40
		}
41
42
		$subFolders = explode('/', $relativePath);
43
44
		if (count($subFolders) > 2) {
45
			$reducedPath = $currFolderPath . $subFolders[0] . '/' . array_pop($subFolders);
46
		} else {
47
			$reducedPath = $path;
48
		}
49
50
		return $reducedPath;
51
	}
52
53
}
54