Conditions | 16 |
Paths | 962 |
Total Lines | 139 |
Code Lines | 92 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
153 | public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) { |
||
154 | if ($fileid !== null) { |
||
155 | try { |
||
156 | return $this->redirectToFile($fileid); |
||
157 | } catch (NotFoundException $e) { |
||
158 | return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $nav = new \OCP\Template('files', 'appnavigation', ''); |
||
163 | |||
164 | // Load the files we need |
||
165 | \OCP\Util::addStyle('files', 'merged'); |
||
166 | \OCP\Util::addScript('files', 'merged-index'); |
||
167 | |||
168 | // mostly for the home storage's free space |
||
169 | // FIXME: Make non static |
||
170 | $storageInfo = $this->getStorageInfo(); |
||
171 | |||
172 | $user = $this->userSession->getUser()->getUID(); |
||
173 | |||
174 | // Get all the user favorites to create a submenu |
||
175 | try { |
||
176 | $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
||
177 | } catch (\RuntimeException $e) { |
||
178 | $favElements['folders'] = []; |
||
179 | } |
||
180 | |||
181 | $collapseClasses = ''; |
||
182 | if (count($favElements['folders']) > 0) { |
||
183 | $collapseClasses = 'collapsible'; |
||
184 | } |
||
185 | |||
186 | $favoritesSublistArray = Array(); |
||
187 | |||
188 | $navBarPositionPosition = 6; |
||
189 | $currentCount = 0; |
||
190 | foreach ($favElements['folders'] as $dir) { |
||
191 | |||
192 | $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']); |
||
193 | $sortingValue = ++$currentCount; |
||
194 | $element = [ |
||
195 | 'id' => str_replace('/', '-', $dir), |
||
196 | 'view' => 'files', |
||
197 | 'href' => $link, |
||
198 | 'dir' => $dir, |
||
199 | 'order' => $navBarPositionPosition, |
||
200 | 'folderPosition' => $sortingValue, |
||
201 | 'name' => basename($dir), |
||
202 | 'icon' => 'files', |
||
203 | 'quickaccesselement' => 'true' |
||
204 | ]; |
||
205 | |||
206 | array_push($favoritesSublistArray, $element); |
||
207 | $navBarPositionPosition++; |
||
208 | } |
||
209 | |||
210 | $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
||
211 | |||
212 | // add the favorites entry in menu |
||
213 | $navItems['favorites']['sublist'] = $favoritesSublistArray; |
||
214 | $navItems['favorites']['classes'] = $collapseClasses; |
||
215 | |||
216 | // parse every menu and add the expandedState user value |
||
217 | foreach ($navItems as $key => $item) { |
||
218 | if (isset($item['expandedState'])) { |
||
219 | $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | $nav->assign('navigationItems', $navItems); |
||
224 | |||
225 | $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
||
226 | if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
||
227 | $totalSpace = $this->l10n->t('Unlimited'); |
||
228 | } else { |
||
229 | $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
||
230 | } |
||
231 | $nav->assign('total_space', $totalSpace); |
||
232 | $nav->assign('quota', $storageInfo['quota']); |
||
233 | $nav->assign('usage_relative', $storageInfo['relative']); |
||
234 | |||
235 | $contentItems = []; |
||
236 | |||
237 | // render the container content for every navigation item |
||
238 | foreach ($navItems as $item) { |
||
239 | $content = ''; |
||
240 | if (isset($item['script'])) { |
||
241 | $content = $this->renderScript($item['appname'], $item['script']); |
||
242 | } |
||
243 | // parse submenus |
||
244 | if (isset($item['sublist'])) { |
||
245 | foreach ($item['sublist'] as $subitem) { |
||
246 | $subcontent = ''; |
||
247 | if (isset($subitem['script'])) { |
||
248 | $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
||
249 | } |
||
250 | $contentItems[$subitem['id']] = [ |
||
251 | 'id' => $subitem['id'], |
||
252 | 'content' => $subcontent |
||
253 | ]; |
||
254 | } |
||
255 | } |
||
256 | $contentItems[$item['id']] = [ |
||
257 | 'id' => $item['id'], |
||
258 | 'content' => $content |
||
259 | ]; |
||
260 | } |
||
261 | |||
262 | $event = new GenericEvent(null, ['hiddenFields' => []]); |
||
263 | $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts', $event); |
||
264 | |||
265 | $params = []; |
||
266 | $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
||
267 | $params['owner'] = $storageInfo['owner']; |
||
268 | $params['ownerDisplayName'] = $storageInfo['ownerDisplayName']; |
||
269 | $params['isPublic'] = false; |
||
270 | $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'); |
||
271 | $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
||
272 | $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
||
273 | $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); |
||
274 | $params['isIE'] = \OCP\Util::isIE(); |
||
275 | $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
||
276 | $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
||
277 | $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
||
278 | $params['appNavigation'] = $nav; |
||
279 | $params['appContents'] = $contentItems; |
||
280 | $params['hiddenFields'] = $event->getArgument('hiddenFields'); |
||
281 | |||
282 | $response = new TemplateResponse( |
||
283 | $this->appName, |
||
284 | 'index', |
||
285 | $params |
||
286 | ); |
||
287 | $policy = new ContentSecurityPolicy(); |
||
288 | $policy->addAllowedFrameDomain('\'self\''); |
||
289 | $response->setContentSecurityPolicy($policy); |
||
290 | |||
291 | return $response; |
||
292 | } |
||
330 |