Completed
Push — enable_class_input ( db22f2...788bee )
by
unknown
20:56
created

ViewController::index()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 24
rs 8.9713
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
69
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
70
		if ($isAssetPipelineEnabled) {
71
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
72
		}
73
74
		$user = $this->userSession->getUser();
75
		$userId = $user->getUID();
76
		$emailAddress = $user->getEMailAddress();
77
78
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
79
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
80
		
81
		$supportsClass = version_compare($runningOn, '9.1', '>=');
82
83
		return new TemplateResponse('calendar', 'main', [
84
			'appVersion' => $appVersion,
85
			'defaultView' => $defaultView,
86
			'emailAddress' => $emailAddress,
87
			'supportsClass' => $supportsClass
88
		]);
89
	}
90
91
	/**
92
	 * @NoAdminRequired
93
	 *
94
	 * @param string $id
95
	 * @return DataDisplayResponse
96
	 */
97
	public function getTimezone($id) {
98
		if (!in_array($id, $this->getTimezoneList())) {
99
			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...
100
		}
101
102
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
103
104
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
105
			'content-type' => 'text/calendar',
106
		]);
107
	}
108
109
110
	/**
111
	 * @NoAdminRequired
112
	 *
113
	 * @param $region
114
	 * @param $city
115
	 * @return DataDisplayResponse
116
	 */
117
	public function getTimezoneWithRegion($region, $city) {
118
		return $this->getTimezone($region . '-' . $city);
119
	}
120
121
122
	/**
123
	 * @NoAdminRequired
124
	 *
125
	 * @param $region
126
	 * @param $subregion
127
	 * @param $city
128
	 * @return DataDisplayResponse
129
	 */
130
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
131
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
132
	}
133
134
135
	/**
136
	 * get a list of default timezones
137
	 *
138
	 * @return array
139
	 */
140
	private function getTimezoneList() {
141
		$allFiles = scandir(__DIR__ . '/../timezones/');
142
143
		return array_values(array_filter($allFiles, function($file) {
144
			return (substr($file, -4) === '.ics');
145
		}));
146
	}
147
}