Completed
Pull Request — master (#32371)
by Tom
12:17 queued 01:17
created

TimezoneController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A set() 0 23 3
1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Settings\Controller;
23
24
use OC\AppFramework\Http;
25
use OCP\AppFramework\Controller;
26
use OCP\AppFramework\Http\JSONResponse;
27
use OCP\IConfig;
28
use OCP\IL10N;
29
use OCP\IRequest;
30
use OCP\IUserSession;
31
use OCP\PreConditionNotMetException;
32
33
/**
34
 * @package OC\Settings\Controller
35
 */
36
class TimezoneController extends Controller {
37
38
	/** @var IRequest */
39
	protected $request;
40
	/** @var IConfig  */
41
	protected $config;
42
	/** @var IL10N  */
43
	protected $l;
44
	/** @var IUserSession  */
45
	protected $session;
46
47
	public function __construct(
48
		$appName,
49
		IRequest $request,
50
		IConfig $config,
51
		IL10N $l,
52
		IUserSession $session
53
	) {
54
		parent::__construct($appName, $request);
55
		$this->request = $request;
56
		$this->config = $config;
57
		$this->l = $l;
58
		$this->session = $session;
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 */
64
	public function set() {
65
66
		$available = \DateTimeZone::listIdentifiers();
67
		if (!in_array($this->request->getParam('timezoneInput'), $available, true)) {
68
			return new JSONResponse(
69
				['message' => $this->l->t('Timezone not recognised')],
70
				Http::STATUS_BAD_REQUEST);
71
		}
72
		try {
73
			$this->config->setUserValue(
74
				$this->session->getUser()->getUID(),
75
				'core',
76
				'timezone',
77
				$this->request->getParam('timezoneInput'));
78
			return new JSONResponse(
79
				['message' => $this->l->t('Done')],
80
				Http::STATUS_ACCEPTED);
81
		} catch (PreConditionNotMetException $e) {
82
			return new JSONResponse(
83
				['message' => $this->l->t('Could not save configuration')],
84
				Http::STATUS_INTERNAL_SERVER_ERROR);
85
		}
86
	}
87
88
}
89