Passed
Push — master ( 35f562...f05fbc )
by Georg
11:07 queued 03:57
created

TimezoneController::getTimezone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2017 Georg Ehrke <[email protected]>
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either
11
 * version 3 of the License, or any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
namespace OCA\Calendar\Controller;
23
24
use OCP\AppFramework\Controller;
25
use OCP\AppFramework\Http;
26
use OCP\AppFramework\Http\DataDisplayResponse;
27
use OCP\AppFramework\Http\NotFoundResponse;
28
29
class TimezoneController extends Controller {
30
31
	/**
32
	 * @NoAdminRequired
33
	 * @PublicPage
34
	 *
35
	 * @param string $id
36
	 * @return NotFoundResponse|DataDisplayResponse
37
	 */
38 4
	public function getTimezone($id) {
39 4
		if (!in_array($id, $this->getTimezoneList())) {
40 2
			return new NotFoundResponse();
41
		}
42
43 2
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
44
45 2
		return new DataDisplayResponse($tzData, Http::STATUS_OK, [
46 2
			'content-type' => 'text/calendar',
47 2
		]);
48
	}
49
50
	/**
51
	 * @NoAdminRequired
52
	 * @PublicPage
53
	 *
54
	 * @param $region
55
	 * @param $city
56
	 * @return DataDisplayResponse
57
	 */
58 2
	public function getTimezoneWithRegion($region, $city) {
59 2
		return $this->getTimezone($region . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($region . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 59 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
60
	}
61
62
	/**
63
	 * @NoAdminRequired
64
	 * @PublicPage
65
	 *
66
	 * @param $region
67
	 * @param $subregion
68
	 * @param $city
69
	 * @return DataDisplayResponse
70
	 */
71
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
72
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($regi...bregion . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 72 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
73
	}
74
75
	/**
76
	 * get a list of default timezones
77
	 *
78
	 * @return array
79
	 */
80 4
	private function getTimezoneList() {
81 4
		$allFiles = scandir(__DIR__ . '/../timezones/');
82
83 4
		return array_values(array_filter($allFiles, function($file) {
84 4
			return (substr($file, -4) === '.ics');
85 4
		}));
86
	}
87
}
88