Completed
Push — master ( 7e1cb7...f254fd )
by korelstar
47:16 queued 35:17
created

SettingsService::get()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.6111
cc 5
nc 8
nop 2
1
<?php
2
3
namespace OCA\Notes\Service;
4
use OCP\AppFramework\Controller;
5
6
use OCP\IConfig;
7
use OCP\IRequest;
8
use OCP\IUserManager;
9
use OCP\IUserSession;
10
use OCP\Files\IRootFolder;
11
use OCP\AppFramework\Http\JSONResponse;
12
13
class SettingsService
14
{
15
	private $config;
16
	private $uid;
17
	private $root;
18
19
	/* Default values */
20
	private $settings = [
21
		"notesPath" => "Notes",
22
	];
23
24
	public function __construct(
25
		IConfig $config,
26
		IUserSession $userSession
27
	) {
28
		$this->config = $config;
29
		$this->uid = $userSession->getUser()->getUID();
30
	}
31
32
	/**
33
	 * @throws \OCP\PreConditionNotMetException
34
	 */
35
	public function set($settings) {
36
		foreach($this->settings as $name => $value) {
37
			$this->settings[$name] = isset($settings[$name]) ? $settings[$name] : $value;
38
		}
39
40
		$this->config->setUserValue($this->uid, 'notes', 'settings', json_encode($this->settings));
41
	}
42
43
	public function get($name = null, $default = null) {
44
		$settings = json_decode($this->config->getUserValue($this->uid, 'notes', 'settings'));
45
		if(!$settings || !is_object($settings)) $settings = $this->settings;
46
		return $name ? (property_exists($settings, $name) ? $settings->{$name} : $default) : $settings;
47
	}
48
}
49