Completed
Pull Request — stable9 (#47)
by Olivier
08:29
created

PathManipulation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 36
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getReducedPath() 0 19 3
1
<?php
2
/**
3
 * ownCloud - galleryplus
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 2015
11
 */
12
13
namespace OCA\GalleryPlus\Controller;
14
15
/**
16
 * @package OCA\GalleryPlus\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 16
	private function getReducedPath($path, $currFolderPath) {
34
		// Adding a slash to make sure we don't cut a folder in half
35 16
		if ($currFolderPath) {
36 13
			$currFolderPath .= '/';
37 13
			$relativePath = str_replace($currFolderPath, '', $path);
38
		} else {
39 3
			$relativePath = $path;
40
		}
41
42 16
		$subFolders = explode('/', $relativePath);
43
44 16
		if (count($subFolders) > 2) {
45 3
			$reducedPath = $currFolderPath . $subFolders[0] . '/' . array_pop($subFolders);
46
		} else {
47 13
			$reducedPath = $path;
48
		}
49
50 16
		return $reducedPath;
51
	}
52
53
}
54