Completed
Pull Request — master (#27730)
by Victor
16:06
created

TemplateLayout   B

Complexity

Total Complexity 25

Size/Duplication

Total Lines 163
Duplicated Lines 11.04 %

Coupling/Cohesion

Components 0
Dependencies 16

Importance

Changes 0
Metric Value
dl 18
loc 163
rs 8.4614
c 0
b 0
f 0
wmc 25
lcom 0
cbo 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findStylesheetFiles() 9 9 1
A findJavascriptFiles() 9 9 1
A convertToRelativePath() 0 8 2
F __construct() 0 105 21

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
 * @author Adam Williamson <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Clark Tomlinson <[email protected]>
7
 * @author Hendrik Leppelsack <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Michael Gapczynski <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Philipp Schaffrath <[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
 * @author Peter Prochaska <[email protected]>
21
 *
22
 * @copyright Copyright (c) 2017, ownCloud GmbH
23
 * @license AGPL-3.0
24
 *
25
 * This code is free software: you can redistribute it and/or modify
26
 * it under the terms of the GNU Affero General Public License, version 3,
27
 * as published by the Free Software Foundation.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32
 * GNU Affero General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU Affero General Public License, version 3,
35
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
36
 *
37
 */
38
namespace OC;
39
40
class TemplateLayout extends \OC_Template {
41
42
	private static $versionHash = '';
43
44
	/**
45
	 * @var \OCP\IConfig
46
	 */
47
	private $config;
48
49
	/**
50
	 * @param string $renderAs
51
	 * @param string $appId application id
52
	 */
53
	public function __construct( $renderAs, $appId = '' ) {
54
55
		// yes - should be injected ....
56
		$this->config = \OC::$server->getConfig();
57
58
		// Decide which page we show
59
		if($renderAs == 'user') {
60
			parent::__construct( 'core', 'layout.user' );
61
			if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
62
				$this->assign('bodyid', 'body-settings');
63
			}else{
64
				$this->assign('bodyid', 'body-user');
65
			}
66
67
			// Code integrity notification
68
			$integrityChecker = \OC::$server->getIntegrityCodeChecker();
69
			if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
70
				\OCP\Util::addScript('core', 'integritycheck-failed-notification');
71
			}
72
73
			// Add navigation entry
74
			$this->assign( 'application', '');
75
			$this->assign( 'appid', $appId );
76
			$navigation = \OC_App::getNavigation();
77
			$this->assign( 'navigation', $navigation);
78
			$settingsNavigation = \OC_App::getSettingsNavigation();
79
			$this->assign( 'settingsnavigation', $settingsNavigation);
80
			foreach($navigation as $entry) {
81
				if ($entry['active']) {
82
					$this->assign( 'application', $entry['name'] );
83
					break;
84
				}
85
			}
86
87
			foreach($settingsNavigation as $entry) {
88
				if ($entry['active']) {
89
					$this->assign( 'application', $entry['name'] );
90
					break;
91
				}
92
			}
93
			$userDisplayName = \OC_User::getDisplayName();
94
			$this->assign('user_displayname', $userDisplayName);
95
			$this->assign('user_uid', \OC_User::getUser());
96
			$this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
97
98
			if (\OC_User::getUser() === false) {
99
				$this->assign('userAvatarSet', false);
100
			} else {
101
				$this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
102
			}
103
104
		} else if ($renderAs == 'error') {
105
			parent::__construct('core', 'layout.guest', '', false);
106
			$this->assign('bodyid', 'body-login');
107
		} else if ($renderAs == 'guest') {
108
			parent::__construct('core', 'layout.guest');
109
			$this->assign('bodyid', 'body-login');
110
		} else {
111
			parent::__construct('core', 'layout.base');
112
113
		}
114
		// Send the language to our layouts
115
		$lang = \OC::$server->getL10NFactory()->findLanguage();
116
		if ($lang === 'sr@latin') {
117
			$lang = 'sr';
118
		}
119
		$this->assign('language', $lang);
120
121
		if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
122
			if (empty(self::$versionHash)) {
123
				$v = \OC_App::getAppVersions();
124
				$v['core'] = implode('.', \OCP\Util::getVersion());
125
				self::$versionHash = md5(implode(',', $v));
126
			}
127
		} else {
128
			self::$versionHash = md5('not installed');
129
		}
130
131
		// Add the js files
132
		$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
133
		$this->assign('jsfiles', []);
134
		if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
135
			$this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
136
		}
137
		foreach($jsFiles as $info) {
138
			$web = $info[1];
139
			$file = $info[2];
140
			$this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
141
		}
142
143
		// Add the css files
144
		$cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
145
		$this->assign('cssfiles', []);
146
		$this->assign('printcssfiles', []);
147
		foreach($cssFiles as $info) {
148
			$web = $info[1];
149
			$file = $info[2];
150
151
			if (substr($file, -strlen('print.css')) === 'print.css') {
152
				$this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
153
			} else {
154
				$this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
155
			}
156
		}
157
	}
158
159
	/**
160
	 * @param array $styles
161
	 * @return array
162
	 */
163 View Code Duplication
	static public function findStylesheetFiles($styles) {
164
		$locator = new \OC\Template\CSSResourceLocator(
165
			\OC::$server->getLogger(),
166
			\OC_Util::getTheme(),
167
			[\OC::$SERVERROOT => \OC::$WEBROOT],
168
			[\OC::$SERVERROOT => \OC::$WEBROOT]);
169
		$locator->find($styles);
170
		return $locator->getResources();
171
	}
172
173
	/**
174
	 * @param array $scripts
175
	 * @return array
176
	 */
177 View Code Duplication
	static public function findJavascriptFiles($scripts) {
178
		$locator = new \OC\Template\JSResourceLocator(
179
			\OC::$server->getLogger(),
180
			\OC_Util::getTheme(),
181
			[\OC::$SERVERROOT => \OC::$WEBROOT],
182
			[\OC::$SERVERROOT => \OC::$WEBROOT]);
183
		$locator->find($scripts);
184
		return $locator->getResources();
185
	}
186
187
	/**
188
	 * Converts the absolute file path to a relative path from \OC::$SERVERROOT
189
	 * @param string $filePath Absolute path
190
	 * @return string Relative path
191
	 * @throws \Exception If $filePath is not under \OC::$SERVERROOT
192
	 */
193
	public static function convertToRelativePath($filePath) {
194
		$relativePath = explode(\OC::$SERVERROOT, $filePath);
195
		if(count($relativePath) !== 2) {
196
			throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
197
		}
198
199
		return $relativePath[1];
200
	}
201
202
}
203