Completed
Push — master ( 35b8f0...ce9739 )
by Joas
15:04 queued 06:11
created

TemplateLayout::__construct()   F

Complexity

Conditions 23
Paths 15876

Size

Total Lines 134
Code Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 23
eloc 94
c 4
b 0
f 1
nc 15876
nop 2
dl 0
loc 134
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Christopher Schäpers <[email protected]>
7
 * @author Clark Tomlinson <[email protected]>
8
 * @author Hendrik Leppelsack <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Jörn Friedrich Dreyer <[email protected]>
11
 * @author Lukas Reschke <[email protected]>
12
 * @author Michael Gapczynski <[email protected]>
13
 * @author Morris Jobke <[email protected]>
14
 * @author Remco Brenninkmeijer <[email protected]>
15
 * @author Robin Appelman <[email protected]>
16
 * @author Robin McCorkell <[email protected]>
17
 * @author Roeland Jago Douma <[email protected]>
18
 * @author Thomas Müller <[email protected]>
19
 * @author Victor Dubiniuk <[email protected]>
20
 *
21
 * @license AGPL-3.0
22
 *
23
 * This code is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License, version 3,
25
 * as published by the Free Software Foundation.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License, version 3,
33
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
34
 *
35
 */
36
namespace OC;
37
38
use OC\Template\JSConfigHelper;
39
use OC\Template\SCSSCacher;
40
41
class TemplateLayout extends \OC_Template {
42
43
	private static $versionHash = '';
44
45
	/**
46
	 * @var \OCP\IConfig
47
	 */
48
	private $config;
49
50
	/**
51
	 * @param string $renderAs
52
	 * @param string $appId application id
53
	 */
54
	public function __construct( $renderAs, $appId = '' ) {
55
56
		// yes - should be injected ....
57
		$this->config = \OC::$server->getConfig();
58
59
		// Decide which page we show
60
		if($renderAs == 'user') {
61
			parent::__construct( 'core', 'layout.user' );
62
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
63
				$this->assign('bodyid', 'body-settings');
64
			}else{
65
				$this->assign('bodyid', 'body-user');
66
			}
67
68
			// Code integrity notification
69
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
70
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
71
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
72
			}
73
74
			// Add navigation entry
75
			$this->assign( 'application', '');
76
			$this->assign( 'appid', $appId );
77
			$navigation = \OC_App::getNavigation();
78
			$this->assign( 'navigation', $navigation);
79
			$settingsNavigation = \OC_App::getSettingsNavigation();
80
			$this->assign( 'settingsnavigation', $settingsNavigation);
81
			foreach($navigation as $entry) {
82
				if ($entry['active']) {
83
					$this->assign( 'application', $entry['name'] );
84
					break;
85
				}
86
			}
87
			
88
			foreach($settingsNavigation as $entry) {
89
				if ($entry['active']) {
90
					$this->assign( 'application', $entry['name'] );
91
					break;
92
				}
93
			}
94
			$userDisplayName = \OC_User::getDisplayName();
95
			$appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
96
			if ($appsMgmtActive) {
97
				$l = \OC::$server->getL10N('lib');
98
				$this->assign('application', $l->t('Apps'));
99
			}
100
			$this->assign('user_displayname', $userDisplayName);
101
			$this->assign('user_uid', \OC_User::getUser());
102
			$this->assign('appsmanagement_active', $appsMgmtActive);
103
			$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
104
105
			if (\OC_User::getUser() === false) {
106
				$this->assign('userAvatarSet', false);
107
			} else {
108
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
109
				$this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
110
			}
111
112
		} else if ($renderAs == 'error') {
113
			parent::__construct('core', 'layout.guest', '', false);
114
			$this->assign('bodyid', 'body-login');
115
		} else if ($renderAs == 'guest') {
116
			parent::__construct('core', 'layout.guest');
117
			$this->assign('bodyid', 'body-login');
118
		} else {
119
			parent::__construct('core', 'layout.base');
120
121
		}
122
		// Send the language to our layouts
123
		$this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
124
125
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
126
			if (empty(self::$versionHash)) {
127
				$v = \OC_App::getAppVersions();
128
				$v['core'] = implode('.', \OCP\Util::getVersion());
129
				self::$versionHash = md5(implode(',', $v));
130
			}
131
		} else {
132
			self::$versionHash = md5('not installed');
133
		}
134
135
		// Add the js files
136
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
137
		$this->assign('jsfiles', array());
138
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
139
			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
140
				$jsConfigHelper = new JSConfigHelper(
141
					\OC::$server->getL10N('core'),
142
					\OC::$server->getThemingDefaults(),
143
					\OC::$server->getAppManager(),
144
					\OC::$server->getSession(),
145
					\OC::$server->getUserSession()->getUser(),
146
					\OC::$server->getConfig(),
147
					\OC::$server->getGroupManager(),
148
					\OC::$server->getIniWrapper(),
149
					\OC::$server->getURLGenerator()
150
				);
151
				$this->assign('inline_ocjs', $jsConfigHelper->getConfig());
152
				$this->assign('foo', 'bar');
153
			} else {
154
				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
155
			}
156
		}
157
		foreach($jsFiles as $info) {
158
			$web = $info[1];
159
			$file = $info[2];
160
			$this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
161
		}
162
163
		// Add the css files and check if server is already installed to prevent
164
		// appdata initialisation before database configuration
165
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
166
			$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
167
		} else {
168
			$cssFiles = array(
169
				[\OC::$SERVERROOT, \OC::$WEBROOT, 'core/css/global.css'],
170
				[\OC::$SERVERROOT, \OC::$WEBROOT, 'core/css/fonts.css'],
171
				[\OC::$SERVERROOT, \OC::$WEBROOT, 'core/css/installation.css']
172
			);
173
		}
174
		$this->assign('cssfiles', array());
175
		$this->assign('printcssfiles', []);
176
		$this->assign('versionHash', self::$versionHash);
177
		foreach($cssFiles as $info) {
178
			$web = $info[1];
179
			$file = $info[2];
180
181
			if (substr($file, -strlen('print.css')) === 'print.css') {
182
				$this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
183
			} else {
184
				$this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix()  );
185
			}
186
		}
187
	}
188
189
	protected function getVersionHashSuffix() {
190
		if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
191
			// allows chrome workspace mapping in debug mode
192
			return "";
193
		}
194
195
		return '?v=' . self::$versionHash;
196
	}
197
198
	/**
199
	 * @param array $styles
200
	 * @return array
201
	 */
202
	static public function findStylesheetFiles($styles) {
203
		// Read the selected theme from the config file
204
		$theme = \OC_Util::getTheme();
205
206
		$SCSSCacher = new SCSSCacher(
207
			\OC::$server->getLogger(),
208
			\OC::$server->getAppDataDir('css'),
209
			\OC::$server->getURLGenerator(),
210
			\OC::$server->getSystemConfig()
211
		);
212
213
		$locator = new \OC\Template\CSSResourceLocator(
214
			\OC::$server->getLogger(),
215
			$theme,
216
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
217
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
218
			$SCSSCacher);
219
		$locator->find($styles);
220
		return $locator->getResources();
221
	}
222
223
	/**
224
	 * @param array $scripts
225
	 * @return array
226
	 */
227
	static public function findJavascriptFiles($scripts) {
228
		// Read the selected theme from the config file
229
		$theme = \OC_Util::getTheme();
230
231
		$locator = new \OC\Template\JSResourceLocator(
232
			\OC::$server->getLogger(),
233
			$theme,
234
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
235
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
236
		$locator->find($scripts);
237
		return $locator->getResources();
238
	}
239
240
	/**
241
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
242
	 * @param string $filePath Absolute path
243
	 * @return string Relative path
244
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
245
	 */
246
	public static function convertToRelativePath($filePath) {
247
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
248
		if(count($relativePath) !== 2) {
249
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
250
		}
251
252
		return $relativePath[1];
253
	}
254
}
255