Completed
Push — public-sharing ( e43a2f...b722a4 )
by Georg
20:08
created

ViewController::getTimezoneWithSubRegion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * ownCloud - Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OC\AppFramework\Http;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\DataDisplayResponse;
29
use OCP\AppFramework\Http\NotFoundResponse;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IRequest;
33
use OCP\IUserSession;
34
35
class ViewController extends Controller {
36
37
	/**
38
	 * @var IConfig
39
	 */
40
	private $config;
41
42
	/**
43
	 * @var IUserSession
44
	 */
45
	private $userSession;
46
47
	/**
48
	 * @param string $appName
49
	 * @param IRequest $request an instance of the request
50
	 * @param IConfig $config
51
	 * @param IUserSession $userSession
52
	 */
53
	public function __construct($appName, IRequest $request,
54
								IUserSession $userSession, IConfig $config) {
55
		parent::__construct($appName, $request);
56
		$this->config = $config;
57
		$this->userSession = $userSession;
58
	}
59
60
	/**
61
	 * @NoAdminRequired
62
	 * @NoCSRFRequired
63
	 *
64
	 * @return TemplateResponse
65
	 */
66
	public function index() {
67
		$runningOn = $this->config->getSystemValue('version');
68
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
69
70
		$supportsClass = $runningOnServer91OrLater;
71
		$assetPipelineBroken = !$runningOnServer91OrLater;
72
73
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
74
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
75
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76
		}
77
78
		$user = $this->userSession->getUser();
79
		$userId = $user->getUID();
80
		$emailAddress = $user->getEMailAddress();
81
82
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
83
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
84
		
85
		return new TemplateResponse('calendar', 'main', [
86
			'appVersion' => $appVersion,
87
			'defaultView' => $defaultView,
88
			'emailAddress' => $emailAddress,
89
			'supportsClass' => $supportsClass,
90
			'isPublic' => false,
91
		]);
92
	}
93
94
	/**
95
	 * @PublicPage
96
	 * @NoCSRFRequired
97
	 *
98
	 * @return TemplateResponse
99
	 */
100
	public function publicIndex() {
101
		$runningOn = $this->config->getSystemValue('version');
102
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
103
104
		$supportsClass = $runningOnServer91OrLater;
105
		$assetPipelineBroken = !$runningOnServer91OrLater;
106
107
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
108
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
109
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
110
		}
111
112
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
113
114
		$response = new TemplateResponse('calendar', 'main', [
115
			'appVersion' => $appVersion,
116
			'defaultView' => 'month',
117
			'emailAddress' => '',
118
			'supportsClass' => $supportsClass,
119
			'isPublic' => true,
120
		], 'public');
121
		$response->addHeader('X-Frame-Options', 'ALLOW');
122
		$csp = new ContentSecurityPolicy();
123
		$csp->addAllowedScriptDomain('*');
124
		$response->setContentSecurityPolicy($csp);
125
126
		return $response;
127
	}
128
129
	/**
130
	 * @NoAdminRequired
131
	 *
132
	 * @param string $id
133
	 * @return DataDisplayResponse
134
	 */
135
	public function getTimezone($id) {
136
		if (!in_array($id, $this->getTimezoneList())) {
137
			return new NotFoundResponse();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...ttp\NotFoundResponse(); (OCP\AppFramework\Http\NotFoundResponse) is incompatible with the return type documented by OCA\Calendar\Controller\...Controller::getTimezone of type OCP\AppFramework\Http\DataDisplayResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
138
		}
139
140
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
141
142
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
143
			'content-type' => 'text/calendar',
144
		]);
145
	}
146
147
148
	/**
149
	 * @NoAdminRequired
150
	 * @NoCSRFRequired
151
	 * @PublicPage
152
	 *
153
	 * @param $region
154
	 * @param $city
155
	 * @return DataDisplayResponse
156
	 */
157
	public function getTimezoneWithRegion($region, $city) {
158
		return $this->getTimezone($region . '-' . $city);
159
	}
160
161
162
	/**
163
	 * @NoAdminRequired
164
	 * @PublicPage
165
	 * @NoCSRFRequired
166
	 *
167
	 * @param $region
168
	 * @param $subregion
169
	 * @param $city
170
	 * @return DataDisplayResponse
171
	 */
172
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
173
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
174
	}
175
176
177
	/**
178
	 * get a list of default timezones
179
	 *
180
	 * @return array
181
	 */
182
	private function getTimezoneList() {
183
		$allFiles = scandir(__DIR__ . '/../timezones/');
184
185
		return array_values(array_filter($allFiles, function($file) {
186
			return (substr($file, -4) === '.ics');
187
		}));
188
	}
189
}
190