| Total Complexity | 48 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 29 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DefaultDashboardProvider 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 DefaultDashboardProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class DefaultDashboardProvider implements DashboardWelcomeQuicklinksProvider |
||
| 29 | { |
||
| 30 | use Configurable; |
||
| 31 | |||
| 32 | public function provideDashboardWelcomeQuicklinks(): array |
||
| 33 | { |
||
| 34 | $this->addPagesLinks(); |
||
| 35 | $this->addFindPages(); |
||
| 36 | $this->addFilesAndImages(); |
||
| 37 | $this->addSiteConfigLinks(); |
||
| 38 | if (Permission::check('ADMIN')) { |
||
| 39 | $this->addSecurityLinks(); |
||
| 40 | } |
||
| 41 | $this->addModelAdminLinks(); |
||
| 42 | $this->addMeLinks(); |
||
| 43 | return DashboardWelcomeQuicklinks::get_links(); |
||
| 44 | } |
||
| 45 | |||
| 46 | private static $model_admins_to_skip = [ |
||
| 47 | ArchiveAdmin::class, |
||
| 48 | ]; |
||
| 49 | |||
| 50 | private static $pages_to_skip = []; |
||
| 51 | |||
| 52 | protected function addPagesLinks() |
||
| 53 | { |
||
| 54 | DashboardWelcomeQuicklinks::add_group('PAGES', 'Pages', 10); |
||
| 55 | $pagesCount = DataObject::get('Page')->count(); |
||
| 56 | $draftCount = CMSSiteTreeFilter_StatusDraftPages::create()->getFilteredPages()->count(); |
||
| 57 | $revisedCount = CMSSiteTreeFilter_ChangedPages::create()->getFilteredPages()->count(); |
||
| 58 | $add = [ |
||
| 59 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'), |
||
| 60 | 'Link' => '/admin/pages/add', |
||
| 61 | ]; |
||
| 62 | DashboardWelcomeQuicklinks::add_link( |
||
| 63 | 'PAGES', |
||
| 64 | DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Pages (' . $pagesCount . ')', |
||
| 65 | '/admin/pages', |
||
| 66 | $add |
||
| 67 | ); |
||
| 68 | DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Unpublished Drafts (' . $draftCount . ')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDraftPages'); |
||
| 69 | DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Unpublished Changes (' . $revisedCount . ')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages'); |
||
| 70 | $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC'); |
||
| 71 | if ($pageLastEdited) { |
||
| 72 | DashboardWelcomeQuicklinks::add_link('PAGES', '✎ Last Edited Page: ' . $pageLastEdited->Title, $pageLastEdited->CMSEditLink()); |
||
| 73 | } |
||
| 74 | $lastWeekLink = '/admin/pages?q[LastEditedFrom]=' . date('Y-m-d', strtotime('-1 week')); |
||
| 75 | DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Recently Modified Pages', $lastWeekLink); |
||
| 76 | DashboardWelcomeQuicklinks::add_link('PAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Page Reports', '/admin/reports'); |
||
| 77 | } |
||
| 78 | |||
| 79 | protected function addFindPages() |
||
| 80 | { |
||
| 81 | $pages = []; |
||
| 82 | $pagesToSkip = (array) $this->Config()->get('pages_to_skip'); |
||
| 83 | foreach (ClassInfo::subclassesFor(SiteTree::class, false) as $className) { |
||
| 84 | if (in_array($className, $pagesToSkip)) { |
||
| 85 | continue; |
||
| 86 | } |
||
| 87 | $pages[$className] = $className; |
||
| 88 | } |
||
| 89 | DashboardWelcomeQuicklinks::add_group('PAGEFILTER', 'Page Types (' . count($pages) . ')', 300); |
||
| 90 | $pagesArray = []; |
||
| 91 | foreach ($pages as $pageClassName) { |
||
| 92 | $pageCount = $pageClassName::get()->filter(['ClassName' => $pageClassName])->count(); |
||
| 93 | if ($pageCount < 1) { |
||
| 94 | $page = Injector::inst()->get($pageClassName); |
||
| 95 | if ($page->canCreate()) { |
||
| 96 | $pageTitle = $page->i18n_singular_name(); |
||
| 97 | $pagesArray[] = [ |
||
| 98 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add') . ' ' . $pageTitle . ' (0)', |
||
| 99 | 'Link' => 'admin/pages/add?PageType=' . $pageClassName, |
||
| 100 | 'Count' => 0, |
||
| 101 | 'InsideLink' => [], |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | } elseif ($pageCount === 1) { |
||
| 105 | $page = DataObject::get_one($pageClassName, ['ClassName' => $pageClassName]); |
||
| 106 | if ($page->canEdit()) { |
||
| 107 | $pageTitle = $page->i18n_singular_name(); |
||
| 108 | $insideLink = []; |
||
| 109 | if ($page->canCreate() && $page->config()->get('can_be_root')) { |
||
| 110 | $insideLink = [ |
||
| 111 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'), |
||
| 112 | 'Link' => 'admin/pages/add?PageType=' . $pageClassName, |
||
| 113 | ]; |
||
| 114 | } |
||
| 115 | $pagesArray[] = [ |
||
| 116 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $pageTitle . ' (1)', |
||
| 117 | 'Link' => $page->CMSEditLink(), |
||
| 118 | 'Count' => 1, |
||
| 119 | 'InsideLink' => $insideLink, |
||
| 120 | ]; |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | $page = Injector::inst()->get($pageClassName); |
||
| 124 | if ($page->canEdit()) { |
||
| 125 | $pageTitle = $page->i18n_plural_name(); |
||
| 126 | $query = 'q[ClassName]=' . $pageClassName; |
||
| 127 | $link = 'admin/pages?' . $query; |
||
| 128 | $insideLink = []; |
||
| 129 | if ($page->canCreate() && $page->config()->get('can_be_root')) { |
||
| 130 | $insideLink = [ |
||
| 131 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'), |
||
| 132 | 'Link' => 'admin/pages/add?PageType=' . $pageClassName, |
||
| 133 | ]; |
||
| 134 | } |
||
| 135 | $pagesArray[] = [ |
||
| 136 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $pageTitle . ' (' . $pageCount . ')', |
||
| 137 | 'Link' => $link, |
||
| 138 | 'Count' => $pageCount, |
||
| 139 | 'InsideLink' => $insideLink, |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $pagesArray = $this->sortByCountAndTitle($pagesArray); |
||
| 145 | foreach ($pagesArray as $pageArray) { |
||
| 146 | DashboardWelcomeQuicklinks::add_link('PAGEFILTER', $pageArray['Title'], $pageArray['Link'], $pageArray['InsideLink']); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | protected function addFilesAndImages() |
||
| 151 | { |
||
| 152 | // 'Files ('.$filesCount.') and Images ('.$imageCount.')' |
||
| 153 | DashboardWelcomeQuicklinks::add_group('FILESANDIMAGES', 'Files and Images', 20); |
||
| 154 | $uploadFolderName = Config::inst()->get(Upload::class, 'uploads_folder'); |
||
| 155 | $uploadFolder = Folder::find_or_make($uploadFolderName); |
||
| 156 | // all |
||
| 157 | DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' Open File Browswer', '/admin/assets'); |
||
| 158 | // per type |
||
| 159 | $filesCount = File::get()->excludeAny(['ClassName' => [Folder::class, Image::class]])->count(); |
||
| 160 | $imageCount = File::get()->filter(['ClassName' => Image::class])->exclude(['ClassName' => Folder::class])->count(); |
||
| 161 | DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Files (' . $filesCount . ')', 'admin/assets?filter[appCategory]=DOCUMENT'); |
||
| 162 | DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Images (' . $imageCount . ')', 'admin/assets?filter[appCategory]=IMAGE'); |
||
| 163 | |||
| 164 | // default upload folder |
||
| 165 | DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Open Default Upload Folder', $uploadFolder->CMSEditLink()); |
||
| 166 | |||
| 167 | // recent |
||
| 168 | $lastWeekLink = '/admin/assets?filter[lastEditedFrom]=' . date('Y-m-d', strtotime('-1 week')) . '&view=tile'; |
||
| 169 | DashboardWelcomeQuicklinks::add_link('FILESANDIMAGES', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Recently modified Files', $lastWeekLink); |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function addSiteConfigLinks() |
||
| 173 | { |
||
| 174 | DashboardWelcomeQuicklinks::add_group('SITECONFIG', 'Site Wide Configuration', 20); |
||
| 175 | DashboardWelcomeQuicklinks::add_link('SITECONFIG', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Site Settings', '/admin/settings'); |
||
| 176 | } |
||
| 177 | |||
| 178 | protected function addSecurityLinks() |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | protected function addModelAdminLinks() |
||
| 231 | { |
||
| 232 | $modelAdmins = []; |
||
| 233 | $skips = (array) $this->Config()->get('model_admins_to_skip'); |
||
| 234 | foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) { |
||
| 235 | if (in_array($className, $skips)) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | $modelAdmins[$className] = $className; |
||
| 239 | } |
||
| 240 | foreach ($modelAdmins as $modelAdminClassName) { |
||
| 241 | $groupAdded = false; |
||
| 242 | $ma = Injector::inst()->get($modelAdminClassName); |
||
| 243 | if ($ma->canView()) { |
||
| 244 | $mas = $ma->getManagedModels(); |
||
| 245 | if (count($mas) > 0) { |
||
| 246 | $numberOfModels = count($mas); |
||
| 247 | $groupCode = strtoupper($modelAdminClassName); |
||
| 248 | $count = 0; |
||
| 249 | foreach ($mas as $model => $title) { |
||
| 250 | $count++; |
||
| 251 | if (is_array($title)) { |
||
| 252 | $title = $title['title']; |
||
| 253 | $model = $title['dataClass'] ?? $model; |
||
| 254 | } |
||
| 255 | if (! class_exists($model)) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | $obj = Injector::inst()->get($model); |
||
| 259 | if ($obj && $obj->canView()) { |
||
| 260 | if (! $groupAdded) { |
||
| 261 | DashboardWelcomeQuicklinks::add_group($groupCode, $ma->menu_title(), 100); |
||
| 262 | $groupAdded = true; |
||
| 263 | } |
||
| 264 | // $classNameList = ClassInfo::subclassesFor($model); |
||
| 265 | $ma = ReflectionHelper::allowAccessToProperty(get_class($ma), 'modelClass'); |
||
| 266 | $ma->modelClass = $model; |
||
| 267 | $list = $ma->getList(); |
||
| 268 | if (! $list) { |
||
| 269 | $list = $model::get(); |
||
| 270 | } |
||
| 271 | $objectCount = $list->count(); |
||
| 272 | if ($objectCount === 1) { |
||
| 273 | $baseTable = Injector::inst()->get($model)->baseTable(); |
||
| 274 | $obj = DataObject::get_one($model, [$baseTable . '.ClassName' => $model]); |
||
| 275 | if (! $obj) { |
||
| 276 | $obj = DataObject::get_one($model); |
||
| 277 | } |
||
| 278 | if ($obj && $obj->hasMethod('CMSEditLink')) { |
||
| 279 | $link = $obj->CMSEditLink(); |
||
| 280 | if ($link) { |
||
| 281 | DashboardWelcomeQuicklinks::add_link($groupCode, DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $model::singleton()->i18n_singular_name(), $link); |
||
| 282 | continue; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | $link = ''; |
||
| 288 | if ($obj->hasMethod('CMSListLink')) { |
||
| 289 | $link = $obj->CMSListLink(); |
||
| 290 | } |
||
| 291 | if (! $link) { |
||
| 292 | $link = $ma->getLinkForModelTab($model); |
||
| 293 | } |
||
| 294 | $titleContainsObjectCount = strpos($title, ' (' . $objectCount . ')'); |
||
| 295 | if ($titleContainsObjectCount === false) { |
||
| 296 | $title .= ' (' . $objectCount . ')'; |
||
| 297 | } |
||
| 298 | $add = []; |
||
| 299 | if ($obj->canCreate()) { |
||
| 300 | $classNameEscaped = str_replace('\\', '-', $model); |
||
| 301 | $linkNew = $link . '/EditForm/field/' . $classNameEscaped . '/item/new'; |
||
| 302 | $add = [ |
||
| 303 | 'Title' => DashboardWelcomeQuicklinks::get_base_phrase('add'), |
||
| 304 | 'Link' => $linkNew, |
||
| 305 | ]; |
||
| 306 | } |
||
| 307 | DashboardWelcomeQuicklinks::add_link( |
||
| 308 | $groupCode, |
||
| 309 | DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' ' . $title, |
||
| 310 | $link, |
||
| 311 | $add |
||
| 312 | ); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | |||
| 320 | protected function addMeLinks() |
||
| 321 | { |
||
| 322 | DashboardWelcomeQuicklinks::add_group('ME', 'My Account', 200); |
||
| 323 | DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('edit') . ' My Details', '/admin/myprofile'); |
||
| 324 | DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Test Password Reset', 'Security/lostpassword'); |
||
| 325 | DashboardWelcomeQuicklinks::add_link('ME', DashboardWelcomeQuicklinks::get_base_phrase('review') . ' Log-out', '/Security/logout'); |
||
| 326 | } |
||
| 327 | |||
| 328 | protected function sortByCountAndTitle(array $array): array |
||
| 339 | } |
||
| 340 | } |
||
| 341 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths