Completed
Pull Request — master (#93)
by Janis
11:26
created

AppConfigService::setAppValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code 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 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
/**
4
 * Nextcloud - OCR
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Janis Koehr <[email protected]>
10
 * @copyright Janis Koehr 2017
11
 */
12
namespace OCA\Ocr\Service;
13
14
use \OCP\IConfig;
15
use OCP\IL10N;
16
17
class AppConfigService {
18
	
19
	/**
20
	 *
21
	 * @var string
22
	 */
23
	private $appName = 'ocr';
24
	
25
	/**
26
	 *
27
	 * @var IConfig
28
	 */
29
	private $config;
30
	
31
	/**
32
	 *
33
	 * @var IL10N
34
	 */
35
	private $l10n;
36
	
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param IConfig $config        	
41
	 * @param IL10N $l10n        	
42
	 */
43
	public function __construct(IConfig $config, IL10N $l10n) {
44
		$this->config = $config;
45
		$this->l10n = $l10n;
46
	}
47
	/**
48
	 * Get a value by key
49
	 *
50
	 * @param string $key        	
51
	 * @return string
52
	 */
53
	public function getAppValue($key) {
54
		return $this->config->getAppValue ( $this->appName, $key );
55
	}
56
	/**
57
	 * Set a value by key
58
	 *
59
	 * @param string $key        	
60
	 * @param string $value        	
61
	 * @return string
62
	 */
63
	public function setAppValue($key, $value) {
64
		if ($key === 'languages') {
65
			if (! preg_match ( '/^(([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4});)*([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4})$/', $value )) {
66
				throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) );
67
			}
68
		}
69
		return $this->config->setAppValue ( $this->appName, $key, $value );
70
	}
71
}