Completed
Push — fix_untranslated_strings ( 07522f...8348da )
by
unknown
17:53
created

ViewController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 108
rs 10
wmc 8
lcom 2
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTimezone() 0 11 2
A getTimezoneWithRegion() 0 3 1
A getTimezoneList() 0 7 1
A getTimezoneWithSubRegion() 0 3 1
A index() 0 19 2
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\JSONResponse;
30
use OCP\AppFramework\Http\NotFoundResponse;
31
use OCP\AppFramework\Http\TemplateResponse;
32
use OCP\IConfig;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
36
class ViewController extends Controller {
37
38
	/**
39
	 * @var IConfig
40
	 */
41
	private $config;
42
43
	/**
44
	 * @var IUserSession
45
	 */
46
	private $userSession;
47
48
	/**
49
	 * @param string $appName
50
	 * @param IRequest $request an instance of the request
51
	 * @param IConfig $config
52
	 * @param IUserSession $userSession
53
	 */
54
	public function __construct($appName, IRequest $request,
55
								IUserSession $userSession, IConfig $config) {
56
		parent::__construct($appName, $request);
57
		$this->config = $config;
58
		$this->userSession = $userSession;
59
	}
60
61
	/**
62
	 * @NoAdminRequired
63
	 * @NoCSRFRequired
64
	 *
65
	 * @return TemplateResponse
66
	 */
67
	public function index() {
68
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
69
		if ($isAssetPipelineEnabled) {
70
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
71
		}
72
73
		$user = $this->userSession->getUser();
74
		$userId = $user->getUID();
75
		$emailAddress = $user->getEMailAddress();
76
77
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
78
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
79
80
		return new TemplateResponse('calendar', 'main', [
81
			'appVersion' => $appVersion,
82
			'defaultView' => $defaultView,
83
			'emailAddress' => $emailAddress,
84
		]);
85
	}
86
87
	/**
88
	 * @NoAdminRequired
89
	 *
90
	 * @param string $id
91
	 * @return DataDisplayResponse
92
	 */
93
	public function getTimezone($id) {
94
		if (!in_array($id, $this->getTimezoneList())) {
95
			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...
96
		}
97
98
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
99
100
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
101
			'content-type' => 'text/calendar',
102
		]);
103
	}
104
105
106
	/**
107
	 * @NoAdminRequired
108
	 *
109
	 * @param $region
110
	 * @param $city
111
	 * @return DataDisplayResponse
112
	 */
113
	public function getTimezoneWithRegion($region, $city) {
114
		return $this->getTimezone($region . '-' . $city);
115
	}
116
117
118
	/**
119
	 * @NoAdminRequired
120
	 *
121
	 * @param $region
122
	 * @param $subregion
123
	 * @param $city
124
	 * @return DataDisplayResponse
125
	 */
126
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
127
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
128
	}
129
130
131
	/**
132
	 * get a list of default timezones
133
	 *
134
	 * @return array
135
	 */
136
	private function getTimezoneList() {
137
		$allFiles = scandir(__DIR__ . '/../timezones/');
138
139
		return array_values(array_filter($allFiles, function($file) {
140
			return (substr($file, -4) === '.ics');
141
		}));
142
	}
143
}