| Total Complexity | 56 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NavigationManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NavigationManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class NavigationManager implements INavigationManager { |
||
| 49 | protected $entries = []; |
||
| 50 | protected $closureEntries = []; |
||
| 51 | protected $activeEntry; |
||
| 52 | protected $unreadCounters = []; |
||
| 53 | |||
| 54 | /** @var bool */ |
||
| 55 | protected $init = false; |
||
| 56 | /** @var IAppManager|AppManager */ |
||
| 57 | protected $appManager; |
||
| 58 | /** @var IURLGenerator */ |
||
| 59 | private $urlGenerator; |
||
| 60 | /** @var IFactory */ |
||
| 61 | private $l10nFac; |
||
| 62 | /** @var IUserSession */ |
||
| 63 | private $userSession; |
||
| 64 | /** @var IGroupManager|Manager */ |
||
| 65 | private $groupManager; |
||
| 66 | /** @var IConfig */ |
||
| 67 | private $config; |
||
| 68 | |||
| 69 | public function __construct(IAppManager $appManager, |
||
| 70 | IURLGenerator $urlGenerator, |
||
| 71 | IFactory $l10nFac, |
||
| 72 | IUserSession $userSession, |
||
| 73 | IGroupManager $groupManager, |
||
| 74 | IConfig $config) { |
||
| 75 | $this->appManager = $appManager; |
||
| 76 | $this->urlGenerator = $urlGenerator; |
||
| 77 | $this->l10nFac = $l10nFac; |
||
| 78 | $this->userSession = $userSession; |
||
| 79 | $this->groupManager = $groupManager; |
||
| 80 | $this->config = $config; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritDoc |
||
| 85 | */ |
||
| 86 | public function add($entry) { |
||
| 87 | if ($entry instanceof \Closure) { |
||
| 88 | $this->closureEntries[] = $entry; |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | $entry['active'] = false; |
||
| 93 | if (!isset($entry['icon'])) { |
||
| 94 | $entry['icon'] = ''; |
||
| 95 | } |
||
| 96 | if (!isset($entry['classes'])) { |
||
| 97 | $entry['classes'] = ''; |
||
| 98 | } |
||
| 99 | if (!isset($entry['type'])) { |
||
| 100 | $entry['type'] = 'link'; |
||
| 101 | } |
||
| 102 | |||
| 103 | $id = $entry['id']; |
||
| 104 | $entry['unread'] = isset($this->unreadCounters[$id]) ? $this->unreadCounters[$id] : 0; |
||
| 105 | |||
| 106 | $this->entries[$id] = $entry; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @inheritDoc |
||
| 111 | */ |
||
| 112 | public function getAll(string $type = 'link'): array { |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Sort navigation entries by order, name and set active flag |
||
| 131 | * |
||
| 132 | * @param array $list |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | private function proceedNavigation(array $list): array { |
||
| 136 | uasort($list, function ($a, $b) { |
||
| 137 | if (isset($a['order']) && isset($b['order'])) { |
||
| 138 | return ($a['order'] < $b['order']) ? -1 : 1; |
||
| 139 | } elseif (isset($a['order']) || isset($b['order'])) { |
||
| 140 | return isset($a['order']) ? -1 : 1; |
||
| 141 | } else { |
||
| 142 | return ($a['name'] < $b['name']) ? -1 : 1; |
||
| 143 | } |
||
| 144 | }); |
||
| 145 | |||
| 146 | $activeApp = $this->getActiveEntry(); |
||
| 147 | if ($activeApp !== null) { |
||
|
|
|||
| 148 | foreach ($list as $index => &$navEntry) { |
||
| 149 | if ($navEntry['id'] == $activeApp) { |
||
| 150 | $navEntry['active'] = true; |
||
| 151 | } else { |
||
| 152 | $navEntry['active'] = false; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | unset($navEntry); |
||
| 156 | } |
||
| 157 | |||
| 158 | return $list; |
||
| 159 | } |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * removes all the entries |
||
| 164 | */ |
||
| 165 | public function clear($loadDefaultLinks = true) { |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritDoc |
||
| 173 | */ |
||
| 174 | public function setActiveEntry($id) { |
||
| 175 | $this->activeEntry = $id; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | public function getActiveEntry() { |
||
| 183 | } |
||
| 184 | |||
| 185 | private function init() { |
||
| 186 | if ($this->init) { |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | $this->init = true; |
||
| 190 | |||
| 191 | $l = $this->l10nFac->get('lib'); |
||
| 192 | if ($this->config->getSystemValue('knowledgebaseenabled', true)) { |
||
| 193 | $this->add([ |
||
| 194 | 'type' => 'settings', |
||
| 195 | 'id' => 'help', |
||
| 196 | 'order' => 6, |
||
| 197 | 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'), |
||
| 198 | 'name' => $l->t('Help'), |
||
| 199 | 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'), |
||
| 200 | ]); |
||
| 201 | } |
||
| 202 | |||
| 203 | if ($this->userSession->isLoggedIn()) { |
||
| 204 | if ($this->isAdmin()) { |
||
| 205 | // App management |
||
| 206 | $this->add([ |
||
| 207 | 'type' => 'settings', |
||
| 208 | 'id' => 'core_apps', |
||
| 209 | 'order' => 4, |
||
| 210 | 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'), |
||
| 211 | 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'), |
||
| 212 | 'name' => $l->t('Apps'), |
||
| 213 | ]); |
||
| 214 | } |
||
| 215 | |||
| 216 | // Personal and (if applicable) admin settings |
||
| 217 | $this->add([ |
||
| 218 | 'type' => 'settings', |
||
| 219 | 'id' => 'settings', |
||
| 220 | 'order' => 2, |
||
| 221 | 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'), |
||
| 222 | 'name' => $l->t('Settings'), |
||
| 223 | 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'), |
||
| 224 | ]); |
||
| 225 | |||
| 226 | $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator); |
||
| 227 | if ($logoutUrl !== '') { |
||
| 228 | // Logout |
||
| 229 | $this->add([ |
||
| 230 | 'type' => 'settings', |
||
| 231 | 'id' => 'logout', |
||
| 232 | 'order' => 99999, |
||
| 233 | 'href' => $logoutUrl, |
||
| 234 | 'name' => $l->t('Log out'), |
||
| 235 | 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'), |
||
| 236 | ]); |
||
| 237 | } |
||
| 238 | |||
| 239 | if ($this->isSubadmin()) { |
||
| 240 | // User management |
||
| 241 | $this->add([ |
||
| 242 | 'type' => 'settings', |
||
| 243 | 'id' => 'core_users', |
||
| 244 | 'order' => 5, |
||
| 245 | 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'), |
||
| 246 | 'name' => $l->t('Users'), |
||
| 247 | 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'), |
||
| 248 | ]); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | if ($this->appManager === 'null') { |
||
| 253 | return; |
||
| 254 | } |
||
| 255 | |||
| 256 | if ($this->userSession->isLoggedIn()) { |
||
| 257 | $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser()); |
||
| 258 | } else { |
||
| 259 | $apps = $this->appManager->getInstalledApps(); |
||
| 260 | } |
||
| 261 | |||
| 262 | foreach ($apps as $app) { |
||
| 263 | if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | // load plugins and collections from info.xml |
||
| 268 | $info = $this->appManager->getAppInfo($app); |
||
| 269 | if (!isset($info['navigations']['navigation'])) { |
||
| 270 | continue; |
||
| 271 | } |
||
| 272 | foreach ($info['navigations']['navigation'] as $key => $nav) { |
||
| 273 | if (!isset($nav['name'])) { |
||
| 274 | continue; |
||
| 275 | } |
||
| 276 | if (!isset($nav['route'])) { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all'; |
||
| 280 | if ($role === 'admin' && !$this->isAdmin()) { |
||
| 281 | continue; |
||
| 282 | } |
||
| 283 | $l = $this->l10nFac->get($app); |
||
| 284 | $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key); |
||
| 285 | $order = isset($nav['order']) ? $nav['order'] : 100; |
||
| 286 | $type = isset($nav['type']) ? $nav['type'] : 'link'; |
||
| 287 | $route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : ''; |
||
| 288 | $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg'; |
||
| 289 | foreach ([$icon, "$app.svg"] as $i) { |
||
| 290 | try { |
||
| 291 | $icon = $this->urlGenerator->imagePath($app, $i); |
||
| 292 | break; |
||
| 293 | } catch (\RuntimeException $ex) { |
||
| 294 | // no icon? - ignore it then |
||
| 295 | } |
||
| 296 | } |
||
| 297 | if ($icon === null) { |
||
| 298 | $icon = $this->urlGenerator->imagePath('core', 'default-app-icon'); |
||
| 299 | } |
||
| 300 | |||
| 301 | $this->add([ |
||
| 302 | 'id' => $id, |
||
| 303 | 'order' => $order, |
||
| 304 | 'href' => $route, |
||
| 305 | 'icon' => $icon, |
||
| 306 | 'type' => $type, |
||
| 307 | 'name' => $l->t($nav['name']), |
||
| 308 | ]); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | private function isAdmin() { |
||
| 319 | } |
||
| 320 | |||
| 321 | private function isSubadmin() { |
||
| 327 | } |
||
| 328 | |||
| 329 | public function setUnreadCounter(string $id, int $unreadCounter): void { |
||
| 333 |