Passed
Push — master ( 146319...0ad8dd )
by Nicolaas
02:41
created

DefaultDashboardProvider::addModelAdminLinks()   F

Complexity

Conditions 19
Paths 612

Size

Total Lines 62
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 14
Bugs 0 Features 0
Metric Value
cc 19
eloc 48
c 14
b 0
f 0
nc 612
nop 0
dl 0
loc 62
rs 0.8888

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Sunnysideup\DashboardWelcomeQuicklinks\Api;
4
5
use SilverStripe\Admin\ModelAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\ModelAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Assets\Folder;
9
use SilverStripe\Assets\Upload;
10
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Control...TreeFilter_ChangedPages was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDraftPages;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Control...Filter_StatusDraftPages was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverStripe\Core\ClassInfo;
14
use SilverStripe\Core\Config\Config;
15
use SilverStripe\Core\Config\Configurable;
16
use SilverStripe\Core\Injector\Injector;
17
use SilverStripe\MFA\Report\EnabledMembers;
0 ignored issues
show
Bug introduced by
The type SilverStripe\MFA\Report\EnabledMembers was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SilverStripe\ORM\DataObject;
19
use SilverStripe\Security\DefaultAdminService;
20
use SilverStripe\Security\Group;
21
use SilverStripe\Security\Member;
22
use SilverStripe\Security\Permission;
23
use SilverStripe\VersionedAdmin\ArchiveAdmin;
0 ignored issues
show
Bug introduced by
The type SilverStripe\VersionedAdmin\ArchiveAdmin was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Sunnysideup\DashboardWelcomeQuicklinks\Interfaces\DashboardWelcomeQuickLinksProvider;
25
26
class DefaultDashboardProvider implements DashboardWelcomeQuickLinksProvider
27
{
28
    use Configurable;
29
    protected $links = [];
30
31
    public function provideDashboardWelcomeQuickLinks(): array
32
    {
33
        $this->addPagesLinks();
34
        $this->addFindPages();
35
        $this->addFilesAndImages();
36
        $this->addSiteConfigLinks();
37
        if(Permission::check('ADMIN')) {
38
            $this->addSecurityLinks();
39
        }
40
        $this->addModelAdminLinks();
41
        $this->addMeLinks();
42
        return $this->links;
43
    }
44
45
46
    private static $add_phrase = '+';
0 ignored issues
show
introduced by
The private property $add_phrase is not used, and could be removed.
Loading history...
47
    private static $review_phrase = '☑';
0 ignored issues
show
introduced by
The private property $review_phrase is not used, and could be removed.
Loading history...
48
    private static $edit_phrase = '✎';
0 ignored issues
show
introduced by
The private property $edit_phrase is not used, and could be removed.
Loading history...
49
    private static $model_admins_to_skip = [
0 ignored issues
show
introduced by
The private property $model_admins_to_skip is not used, and could be removed.
Loading history...
50
        ArchiveAdmin::class,
51
    ];
52
    private static $pages_to_skip = [
0 ignored issues
show
introduced by
The private property $pages_to_skip is not used, and could be removed.
Loading history...
53
54
    ];
55
56
57
    protected function addPagesLinks()
58
    {
59
        $this->addGroup('PAGES', 'Pages', 10);
60
        $this->addLink('PAGES', $this->phrase('add'). ' Page', '/admin/pages/add');
61
        $pagesCount = DataObject::get('Page')->count();
62
        $draftCount = CMSSiteTreeFilter_StatusDraftPages::create()->getFilteredPages()->count();
63
        $revisedCount = CMSSiteTreeFilter_ChangedPages::create()->getFilteredPages()->count();
64
        $this->addLink('PAGES', $this->phrase('edit'). ' Pages ('.$pagesCount.')', '/admin/pages');
65
        $this->addLink('PAGES', $this->phrase('edit'). ' Unpublished Drafts ('.$draftCount.')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_StatusDraftPages');
66
        $this->addLink('PAGES', $this->phrase('edit'). ' Unpublished Changes ('.$revisedCount.')', '/admin/pages?q[FilterClass]=SilverStripe\CMS\Controllers\CMSSiteTreeFilter_ChangedPages');
67
        $pageLastEdited = DataObject::get_one('Page', '', true, 'LastEdited DESC');
68
        if ($pageLastEdited) {
69
            $this->addLink('PAGES', '✎ Last Edited Page: '.$pageLastEdited->Title, $pageLastEdited->CMSEditLink());
70
        }
71
        $lastWeekLink = '/admin/pages?'.'q[LastEditedFrom]='.date('Y-m-d', strtotime('-1 week'));
72
        $this->addLink('PAGES', $this->phrase('review'). ' Recently Modified Pages', $lastWeekLink);
73
        $this->addLink('PAGES', $this->phrase('review'). ' Page Reports', '/admin/reports');
74
    }
75
76
    protected function addFindPages()
77
    {
78
        $pages = [];
79
        $pagesToSkip = (array) $this->Config()->get('pages_to_skip');
80
        foreach (ClassInfo::subclassesFor(SiteTree::class, false) as $className) {
81
            if(in_array($className, $pagesToSkip)) {
82
                continue;
83
            }
84
            $pages[$className] = $className;
85
86
        }
87
        $this->addGroup('PAGEFILTER', 'Page Types ('.count($pages).')', 300);
88
        $count = 0;
89
        foreach($pages as $pageClassName) {
90
            $pageCount = $pageClassName::get()->filter(['ClassName' => $pageClassName])->count();
91
            if($pageCount < 1) {
92
                continue;
93
            }
94
            $count++;
95
            if($pageCount === 1) {
96
                $obj = DataObject::get_one($pageClassName, ['ClassName' => $pageClassName]);
97
                $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$pageClassName::singleton()->i18n_singular_name(), $obj->CMSEditLink());
98
                continue;
99
            }
100
            $page = Injector::inst()->get($pageClassName);
101
            $pageTitle = $page->i18n_singular_name();
102
            $query = 'q[ClassName]='.$pageClassName;
103
            $link = 'admin/pages?' . $query;
104
            $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$pageTitle.' ('.$pageCount.')', $link);
105
        }
106
    }
107
    protected function addFilesAndImages()
108
    {
109
        // 'Files ('.$filesCount.') and Images ('.$imageCount.')'
110
        $this->addGroup('FILESANDIMAGES', 'Files and Images', 20);
111
        $uploadFolderName = Config::inst()->get(Upload::class, 'uploads_folder');
112
        $uploadFolder = Folder::find_or_make($uploadFolderName);
113
        // all
114
        $this->addLink('FILESANDIMAGES', $this->phrase('edit'). ' Open File Browswer', '/admin/assets');
115
        // per type
116
        $filesCount = File::get()->exclude(['ClassName' => [Folder::class, Image::class]])->count();
117
        $imageCount = File::get()->filter(['ClassName' => [Image::class]])->count();
118
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Images ('.$imageCount.')', 'admin/assets?filter[appCategory]=IMAGE');
119
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Files ('.$filesCount.')', 'admin/assets?filter[appCategory]=DOCUMENT');
120
121
        // default upload folder
122
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Open Default Upload Folder', $uploadFolder->CMSEditLink());
123
124
        // recent
125
        $lastWeekLink = '/admin/assets?'.'filter[lastEditedFrom]='.date('Y-m-d', strtotime('-1 week')).'&view=tile';
126
        $this->addLink('FILESANDIMAGES', $this->phrase('review'). ' Recently modified Files', $lastWeekLink);
127
    }
128
129
    protected function addSiteConfigLinks()
130
    {
131
        $this->addGroup('SITECONFIG', 'Site Wide Configuration', 20);
132
        $this->addLink('SITECONFIG', $this->phrase('review'). ' Site Settings', '/admin/settings');
133
    }
134
135
    protected function addSecurityLinks()
136
    {
137
        $this->addGroup('SECURITY', 'Security', 30);
138
        $this->addLink('SECURITY', $this->phrase('add'). ' User', '/admin/security/users/EditForm/field/users/item/new');
139
        $userCount = Member::get()->count();
140
        $groupCount = Group::get()->count();
141
        $this->addLink('SECURITY', $this->phrase('review'). ' Users ('.$userCount.')', '/admin/security');
142
        $this->addLink('SECURITY', $this->phrase('review'). ' Groups  ('.$groupCount.')', '/admin/security/groups');
143
        DefaultAdminService::singleton()->extend('addSecurityLinks', $this);
144
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
145
        if($adminGroup) {
146
            $userCount = $adminGroup->Members()->count();
147
            $this->addLink('SECURITY', $this->phrase('review'). ' Administrators ('.$userCount.')', '/admin/security/groups/EditForm/field/groups/item/'.$adminGroup->ID.'/edit');
148
        }
149
        if(class_exists(EnabledMembers::class)) {
150
            $obj = Injector::inst()->get(EnabledMembers::class);
151
            $this->addLink('SECURITY', $this->phrase('review'). ' Multi-Factor Authentication Status', $obj->getLink());
152
153
        }
154
    }
155
156
157
158
159
    protected function addModelAdminLinks()
160
    {
161
        $modelAdmins = [];
162
        $skips = (array) $this->Config()->get('model_admins_to_skip');
163
        foreach (ClassInfo::subclassesFor(ModelAdmin::class, false) as $className) {
164
            if(in_array($className, $skips)) {
165
                continue;
166
            }
167
            $modelAdmins[$className] = $className;
168
169
        }
170
        foreach($modelAdmins as $modelAdminClassName) {
171
            $groupAdded = false;
172
            $ma = Injector::inst()->get($modelAdminClassName);
173
            if($ma->canView()) {
174
                $mas = $ma->getManagedModels();
175
                if(count($mas)) {
176
                    $numberOfModels = count($mas);
177
                    $groupCode = strtoupper($modelAdminClassName);
178
                    $count = 0;
179
                    foreach($mas as $model => $title) {
180
                        $count++;
181
                        if(is_array($title)) {
182
                            $title = $title['title'];
183
                            $model = $title['dataClass'] ?? $model;
184
                        }
185
                        if(! class_exists($model)) {
186
                            continue;
187
                        }
188
                        $obj = Injector::inst()->get($model);
189
                        if($obj && $obj->canView()) {
190
                            if(! $groupAdded) {
191
                                $this->addGroup($groupCode, $ma->menu_title(), 100);
192
                                $groupAdded = true;
193
                            }
194
                            // $classNameList = ClassInfo::subclassesFor($model);
195
                            $objectCount = $model::get()->count();
196
                            if($objectCount === 1) {
197
                                $obj = DataObject::get_one($model, ['ClassName' => $model]);
198
                                if($obj->hasMethod('CMSEditLink')) {
199
                                    $this->addLink('PAGEFILTER', $this->phrase('edit'). ' '.$model::singleton()->i18n_singular_name(), $obj->CMSEditLink());
200
                                    continue;
201
                                }
202
                            }
203
204
                            $link = '';
205
                            if($obj->hasMethod('CMSListLink')) {
206
                                $link = $obj->CMSListLink();
207
                            } if(! $link) {
208
                                $link = $ma->getLinkForModelTab($model);
209
                            }
210
                            $titleContainsObjectCount = strpos($title, ' ('.$objectCount.')');
211
                            if($titleContainsObjectCount === false) {
212
                                $title .= ' ('.$objectCount.')';
213
                            }
214
                            $this->addLink($groupCode, $this->phrase('edit'). ' '.$title, $link);
215
                            if($numberOfModels < 4) {
216
                                $obj = Injector::inst()->get($model);
217
                                if($obj->canCreate()) {
218
                                    $classNameEscaped = str_replace('\\', '-', $model);
219
                                    $linkNew = $link .= '/EditForm/field/'.$classNameEscaped.'/item/new';
220
                                    $this->addLink($groupCode, $this->phrase('add'). ' '.$obj->i18n_singular_name(), $linkNew);
221
                                }
222
                            }
223
                        }
224
                    }
225
                }
226
            }
227
        }
228
    }
229
230
231
232
    protected function addMeLinks()
233
    {
234
        $this->addGroup('ME', 'My Account', 200);
235
        $this->addLink('ME', $this->phrase('edit') . '  My Details', '/admin/myprofile');
236
        $this->addLink('ME', $this->phrase('review') . '  Test Password Reset', 'Security/lostpassword');
237
        $this->addLink('ME', $this->phrase('review') . '  Log-out', '/Security/logout');
238
    }
239
240
241
    protected function addGroup(string $groupCode, string $title, $sort)
242
    {
243
        $this->links[$groupCode] = [
244
            'Title' => $title,
245
            'SortOrdre' => $sort,
246
        ];
247
    }
248
249
    protected function addLink($groupCode, $title, $link)
250
    {
251
        $this->links[$groupCode]['Items'][] = [
252
            'Title' => $title,
253
            'Link' => $link,
254
        ];
255
    }
256
257
    protected function phrase(string $phrase): string
258
    {
259
        $phrase = $this->config()->get($phrase .'_phrase');
260
        return _t('DashboardWelcomeQuicklinks.'.$phrase, $phrase);
261
    }
262
263
}
264