Completed
Pull Request — master (#1917)
by Lukas
40:33 queued 30:26
created

TemplateLayout   C

Complexity

Total Complexity 26

Size/Duplication

Total Lines 187
Duplicated Lines 12.83 %

Coupling/Cohesion

Components 0
Dependencies 21

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 187
rs 6.1111
wmc 26
lcom 0
cbo 21

4 Methods

Rating   Name   Duplication   Size   Complexity  
F __construct() 0 124 22
A findStylesheetFiles() 12 12 1
A findJavascriptFiles() 12 12 1
A convertToRelativePath() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Assetic\Asset\AssetCollection;
39
use Assetic\Asset\FileAsset;
40
use Assetic\AssetWriter;
41
use Assetic\Filter\CssImportFilter;
42
use Assetic\Filter\CssMinFilter;
43
use Assetic\Filter\CssRewriteFilter;
44
use Assetic\Filter\JSqueezeFilter;
45
use Assetic\Filter\SeparatorFilter;
46
use OC\Template\JSConfigHelper;
47
48
class TemplateLayout extends \OC_Template {
49
50
	private static $versionHash = '';
51
52
	/**
53
	 * @var \OCP\IConfig
54
	 */
55
	private $config;
56
57
	/**
58
	 * @param string $renderAs
59
	 * @param string $appId application id
60
	 */
61
	public function __construct( $renderAs, $appId = '' ) {
62
63
		// yes - should be injected ....
64
		$this->config = \OC::$server->getConfig();
65
66
		// Decide which page we show
67
		if($renderAs == 'user') {
68
			parent::__construct( 'core', 'layout.user' );
69
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
70
				$this->assign('bodyid', 'body-settings');
71
			}else{
72
				$this->assign('bodyid', 'body-user');
73
			}
74
75
			// Code integrity notification
76
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
77
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
78
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
79
			}
80
81
			// Add navigation entry
82
			$this->assign( 'application', '');
83
			$this->assign( 'appid', $appId );
84
			$navigation = \OC_App::getNavigation();
85
			$this->assign( 'navigation', $navigation);
86
			$settingsNavigation = \OC_App::getSettingsNavigation();
87
			$this->assign( 'settingsnavigation', $settingsNavigation);
88
			foreach($navigation as $entry) {
89
				if ($entry['active']) {
90
					$this->assign( 'application', $entry['name'] );
91
					break;
92
				}
93
			}
94
			
95
			foreach($settingsNavigation as $entry) {
96
				if ($entry['active']) {
97
					$this->assign( 'application', $entry['name'] );
98
					break;
99
				}
100
			}
101
			$userDisplayName = \OC_User::getDisplayName();
102
			$appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
103
			if ($appsMgmtActive) {
104
				$l = \OC::$server->getL10N('lib');
105
				$this->assign('application', $l->t('Apps'));
106
			}
107
			$this->assign('user_displayname', $userDisplayName);
108
			$this->assign('user_uid', \OC_User::getUser());
109
			$this->assign('appsmanagement_active', $appsMgmtActive);
110
			$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
111
112
			if (\OC_User::getUser() === false) {
113
				$this->assign('userAvatarSet', false);
114
			} else {
115
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
116
				$this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
117
			}
118
119
		} else if ($renderAs == 'error') {
120
			parent::__construct('core', 'layout.guest', '', false);
121
			$this->assign('bodyid', 'body-login');
122
		} else if ($renderAs == 'guest') {
123
			parent::__construct('core', 'layout.guest');
124
			$this->assign('bodyid', 'body-login');
125
		} else {
126
			parent::__construct('core', 'layout.base');
127
128
		}
129
		// Send the language to our layouts
130
		$this->assign('language', \OC_L10N::findLanguage());
0 ignored issues
show
Deprecated Code introduced by
The method OC_L10N::findLanguage() has been deprecated with message: 9.0.0 Use \OC::$server->getL10NFactory()->findLanguage() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
131
132
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
133
			if (empty(self::$versionHash)) {
134
				$v = \OC_App::getAppVersions();
135
				$v['core'] = implode('.', \OCP\Util::getVersion());
136
				self::$versionHash = md5(implode(',', $v));
137
			}
138
		} else {
139
			self::$versionHash = md5('not installed');
140
		}
141
142
		// Add the js files
143
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
144
		$this->assign('jsfiles', array());
145
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
146
			if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
147
				$jsConfigHelper = new JSConfigHelper(
148
					\OC::$server->getL10N('core'),
149
					\OC::$server->getThemingDefaults(),
150
					\OC::$server->getAppManager(),
151
					\OC::$server->getUserSession()->getUser(),
152
					\OC::$server->getConfig(),
153
					\OC::$server->getGroupManager(),
154
					\OC::$server->getIniWrapper(),
155
					\OC::$server->getURLGenerator()
156
				);
157
				$this->assign('inline_ocjs', $jsConfigHelper->getConfig());
158
				$this->assign('foo', 'bar');
159
			} else {
160
				$this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
161
			}
162
		}
163
		foreach($jsFiles as $info) {
164
			$web = $info[1];
165
			$file = $info[2];
166
			$this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
167
		}
168
169
		// Add the css files
170
		$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
171
		$this->assign('cssfiles', array());
172
		$this->assign('printcssfiles', []);
173
		$this->assign('versionHash', self::$versionHash);
174
		foreach($cssFiles as $info) {
175
			$web = $info[1];
176
			$file = $info[2];
177
178
			if (substr($file, -strlen('print.css')) === 'print.css') {
179
				$this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
180
			} else {
181
				$this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
182
			}
183
		}
184
	}
185
186
	/**
187
	 * @param array $styles
188
	 * @return array
189
	 */
190 View Code Duplication
	static public function findStylesheetFiles($styles) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
		// Read the selected theme from the config file
192
		$theme = \OC_Util::getTheme();
193
194
		$locator = new \OC\Template\CSSResourceLocator(
195
			\OC::$server->getLogger(),
196
			$theme,
197
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
198
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
199
		$locator->find($styles);
200
		return $locator->getResources();
201
	}
202
203
	/**
204
	 * @param array $scripts
205
	 * @return array
206
	 */
207 View Code Duplication
	static public function findJavascriptFiles($scripts) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
		// Read the selected theme from the config file
209
		$theme = \OC_Util::getTheme();
210
211
		$locator = new \OC\Template\JSResourceLocator(
212
			\OC::$server->getLogger(),
213
			$theme,
214
			array( \OC::$SERVERROOT => \OC::$WEBROOT ),
215
			array( \OC::$SERVERROOT => \OC::$WEBROOT ));
216
		$locator->find($scripts);
217
		return $locator->getResources();
218
	}
219
220
	/**
221
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
222
	 * @param string $filePath Absolute path
223
	 * @return string Relative path
224
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
225
	 */
226
	public static function convertToRelativePath($filePath) {
227
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
228
		if(count($relativePath) !== 2) {
229
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
230
		}
231
232
		return $relativePath[1];
233
	}
234
}
235