Passed
Push — feature/329_Subsonic_API ( a9dee6...9d1353 )
by Pauli
14:33
created

UserMusicFolder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2019
11
 */
12
13
namespace OCA\Music\Utility;
14
15
use \OCP\Files\Folder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\Folder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use \OCP\Files\IRootFolder;
0 ignored issues
show
Bug introduced by
The type OCP\Files\IRootFolder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use \OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use \OCA\Music\AppFramework\Core\Logger;
20
21
/**
22
 * Manage the user-specific music folder setting
23
 */
24
class UserMusicFolder {
25
26
	private $appName;
27
	private $configManager;
28
	private $rootFolder;
29
	private $logger;
30
31
	public function __construct(
32
			$appName,
33
			IConfig $configManager,
34
			IRootFolder $rootFolder,
35
			Logger $logger) {
36
		$this->appName = $appName;
37
		$this->configManager = $configManager;
38
		$this->rootFolder = $rootFolder;
39
		$this->logger = $logger;
40
	}
41
42
	/**
43
	 * @param string $userId
44
	 * @param string $path
45
	 */
46
	public function setPath($userId, $path) {
47
		$success = false;
48
49
		$userHome = $this->rootFolder->getUserFolder($userId);
50
		$element = $userHome->get($path);
51
		if ($element instanceof \OCP\Files\Folder) {
52
			if ($path[0] !== '/') {
53
				$path = '/' . $path;
54
			}
55
			if ($path[\strlen($path)-1] !== '/') {
56
				$path .= '/';
57
			}
58
			$this->configManager->setUserValue($userId, $this->appName, 'path', $path);
59
			$success = true;
60
		}
61
62
		return $success;
63
	}
64
65
	/**
66
	 * @param string $userId
67
	 * @return string
68
	 */
69
	public function getPath($userId) {
70
		$path = $this->configManager->getUserValue($userId, $this->appName, 'path');
71
		return $path ?: '/';
72
	}
73
74
	/**
75
	 * @param string $userId
76
	 * @return Folder
77
	 */
78
	public function getFolder($userId) {
79
		$userHome = $this->rootFolder->getUserFolder($userId);
80
		$path = $this->getPath($userId);
81
		return Util::getFolderFromRelativePath($userHome, $path);
82
	}
83
}
84