Passed
Pull Request — master (#198)
by Nic
02:19
created

Slide   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 125
c 1
b 0
f 0
dl 0
loc 341
rs 9.2
wmc 40

9 Methods

Rating   Name   Duplication   Size   Complexity  
A canPublish() 0 17 5
C canView() 0 44 14
A getSlideType() 0 3 1
A canEdit() 0 19 5
A getCMSFields() 0 31 1
A providePermissions() 0 26 1
A canCreate() 0 20 6
A getSiteConfig() 0 8 2
A canDelete() 0 22 5

How to fix   Complexity   

Complex Class

Complex classes like Slide 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 Slide, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Dynamic\FlexSlider\Model;
4
5
use DNADesign\Elemental\Forms\TextCheckboxGroupField;
0 ignored issues
show
Bug introduced by
The type DNADesign\Elemental\Forms\TextCheckboxGroupField 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 Dynamic\FlexSlider\Interfaces\SlideInterface;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\Security\InheritedPermissions;
10
use SilverStripe\Security\InheritedPermissionsExtension;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\PermissionProvider;
13
use SilverStripe\Security\Security;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use SilverStripe\Versioned\Versioned;
16
17
/**
18
 * Class Slide
19
 * @package Dynamic\FlexSlider\Model
20
 *
21
 * @mixin Versioned
22
 * @mixin InheritedPermissionsExtension
23
 */
24
class Slide extends DataObject implements PermissionProvider, SlideInterface
25
{
26
    /**
27
     * The Create permission
28
     */
29
    const SLIDE_CREATE = 'SLIDE_CREATE';
30
31
    /**
32
     * The Publish permission
33
     */
34
    const SLIDE_PUBLISH = 'SLIDE_PUBLISH';
35
36
    /**
37
     * The Edit permission
38
     */
39
    const SLIDE_EDIT = 'SLIDE_EDIT';
40
41
    /**
42
     * The Delete permission
43
     */
44
    const SLIDE_DELETE = 'SLIDE_DELETE';
45
46
    /**
47
     * @var string
48
     */
49
    private static $singular_name = 'Slide';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var string
53
     */
54
    private static $plural_name = 'Slides';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
55
56
    /**
57
     * @var string
58
     */
59
    private static $table_name = 'Slide';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
60
61
    /**
62
     * @var array
63
     */
64
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
65
        'Title' => 'Varchar(255)',
66
        'ShowTitle' => 'Boolean',
67
        'Content' => 'HTMLText',
68
        'SortOrder' => 'Int',
69
    ];
70
71
    /**
72
     * @var array
73
     */
74
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
75
        'Page' => \Page::class,
76
    ];
77
78
    /**
79
     * @var array
80
     */
81
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
82
        Versioned::class,
83
        InheritedPermissionsExtension::class,
84
    ];
85
86
    /**
87
     * @var array
88
     */
89
    private static $versioning = [
0 ignored issues
show
introduced by
The private property $versioning is not used, and could be removed.
Loading history...
90
        "Stage", "Live",
91
    ];
92
93
94
    /**
95
     * @var string
96
     */
97
    private static $default_sort = 'SortOrder';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
98
99
    /**
100
     * Adds Publish button to SlideImage record
101
     *
102
     * @var bool
103
     */
104
    private static $versioned_gridfield_extensions = true;
0 ignored issues
show
introduced by
The private property $versioned_gridfield_extensions is not used, and could be removed.
Loading history...
105
106
    /**
107
     * @var array
108
     */
109
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
110
        'Title',
111
        'Content',
112
    ];
113
114
    /**
115
     * @var array
116
     */
117
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
118
        'SlideType',
119
        'Title',
120
    ];
121
122
    /**
123
     * @return FieldList
124
     */
125
    public function getCMSFields()
126
    {
127
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
128
            $viewInherit = $fields->dataFieldByName('CanViewType');
129
            $editInherit = $fields->dataFieldByName('CanEditType');
130
131
            $fields->removeByName([
132
                'Title',
133
                'ShowTitle',
134
                'SortOrder',
135
                'CanViewType',
136
                'CanEditType',
137
                'PageID',
138
            ]);
139
140
            $fields->addFieldToTab(
141
                'Root.Main',
142
                TextCheckboxGroupField::create(),
143
                'Content'
144
            );
145
146
            $fields->addFieldsToTab(
147
                'Root.Permissions',
148
                [
149
                    $viewInherit,
150
                    $editInherit,
151
                ]
152
            );
153
        });
154
155
        return parent::getCMSFields();
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    public function providePermissions()
162
    {
163
        return [
164
            static::SLIDE_CREATE => [
165
                'name' => _t(__CLASS__ . '.PERMISSION_CREATE_DESCRIPTION', 'Create Slides'),
166
                'help' => _t(__CLASS__ . '.PERMISSION_CREATE_HELP', 'Allow creating slides'),
167
                'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'),
168
                'sort' => 100,
169
            ],
170
            static::SLIDE_PUBLISH => [
171
                'name' => _t(__CLASS__ . '.PERMISSION_PUBLISH_DESCRIPTION', 'Publish Slides'),
172
                'help' => _t(__CLASS__ . '.PERMISSION_PUBLISH_HELP', 'Allow publishing slides'),
173
                'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'),
174
                'sort' => 100,
175
            ],
176
            static::SLIDE_EDIT => [
177
                'name' => _t(__CLASS__ . '.PERMISSION_EDIT_DESCRIPTION', 'Edit Slides'),
178
                'help' => _t(__CLASS__ . '.PERMISSION_EDIT_HELP', 'Allow editing slides'),
179
                'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'),
180
                'sort' => 100,
181
            ],
182
            static::SLIDE_DELETE => [
183
                'name' => _t(__CLASS__ . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Slides'),
184
                'help' => _t(__CLASS__ . '.PERMISSION_DELETE_HELP', 'Allow deleting slides'),
185
                'category' => _t(__CLASS__ . '.PERMISSIONS_CATEGORY', 'FlexSlider Permissions'),
186
                'sort' => 100,
187
            ],
188
        ];
189
    }
190
191
    /**
192
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
193
     * @param array $context
194
     * @return bool|int
195
     */
196
    public function canCreate($member = null, $context = [])
197
    {
198
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
199
            $member = Security::getCurrentUser();
200
        }
201
202
        // Standard mechanism for accepting permission changes from extensions
203
        $extended = $this->extendedCan(__FUNCTION__, $member, $context);
204
        if ($extended !== null) {
205
            return $extended;
206
        }
207
208
        // Check permission
209
        if ($member && Permission::checkMember($member, "ADMIN") || Permission::checkMember($member, static::SLIDE_CREATE)) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($member && SilverStripe..., static::SLIDE_CREATE), Probably Intended Meaning: $member && (SilverStripe... static::SLIDE_CREATE))
Loading history...
210
            return true;
211
        }
212
213
        // This doesn't necessarily mean we are creating a root page, but that
214
        // we don't know if there is a parent, so default to this permission
215
        return SiteConfig::current_site_config()->canCreateTopLevel($member);
216
    }
217
218
    /**
219
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
220
     * @return bool|int
221
     */
222
    public function canPublish($member = null)
223
    {
224
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
225
            $member = Security::getCurrentUser();
226
        }
227
228
        // Check extension
229
        $extended = $this->extendedCan('canPublish', $member);
230
        if ($extended !== null) {
231
            return $extended;
232
        }
233
234
        if (Permission::checkMember($member, "ADMIN") || Permission::checkMember($member, static::SLIDE_PUBLISH)) {
235
            return true;
236
        }
237
238
        return false;
239
    }
240
241
    /**
242
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
243
     * @return bool|int
244
     */
245
    public function canEdit($member = null)
246
    {
247
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
248
            $member = Security::getCurrentUser();
249
        }
250
251
        // Standard mechanism for accepting permission changes from extensions
252
        $extended = $this->extendedCan('canEdit', $member);
253
        if ($extended !== null) {
254
            return $extended;
255
        }
256
257
        // Default permissions
258
        if (Permission::checkMember($member, "ADMIN") || Permission::checkMember($member, static::SLIDE_EDIT)) {
259
            return true;
260
        }
261
262
263
        return false;
264
    }
265
266
    /**
267
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
268
     * @return bool|null
269
     */
270
    public function canView($member = null)
271
    {
272
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
273
            $member = Security::getCurrentUser();
274
        }
275
276
        // Standard mechanism for accepting permission changes from extensions
277
        $extended = $this->extendedCan('canView', $member);
278
        if ($extended !== null) {
279
            return $extended;
280
        }
281
282
        // admin override
283
        if ($member && Permission::checkMember($member, ["ADMIN", "SITETREE_VIEW_ALL"])) {
284
            return true;
285
        }
286
287
        // Note: getInheritedPermissions() is disused in this instance
288
        // to allow parent canView extensions to influence subpage canView()
289
290
        // check for empty spec
291
        if (!$this->CanViewType || $this->CanViewType === InheritedPermissions::ANYONE) {
0 ignored issues
show
Bug Best Practice introduced by
The property CanViewType does not exist on Dynamic\FlexSlider\Model\Slide. Since you implemented __get, consider adding a @property annotation.
Loading history...
292
            return true;
293
        }
294
295
        // check for inherit
296
        if ($this->CanViewType === InheritedPermissions::INHERIT) {
297
            return $this->getSiteConfig()->canViewPages($member);
298
        }
299
300
        // check for any logged-in users
301
        if ($this->CanViewType === InheritedPermissions::LOGGED_IN_USERS && $member && $member->ID) {
302
            return true;
303
        }
304
305
        // check for specific groups
306
        if ($this->CanViewType === InheritedPermissions::ONLY_THESE_USERS
307
            && $member
308
            && $member->inGroups($this->ViewerGroups())
0 ignored issues
show
Bug introduced by
The method ViewerGroups() does not exist on Dynamic\FlexSlider\Model\Slide. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

308
            && $member->inGroups($this->/** @scrutinizer ignore-call */ ViewerGroups())
Loading history...
309
        ) {
310
            return true;
311
        }
312
313
        return false;
314
    }
315
316
    /**
317
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
318
     * @return bool|int
319
     */
320
    public function canDelete($member = null)
321
    {
322
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type null, thus it always evaluated to false.
Loading history...
323
            $member = Security::getCurrentUser();
324
        }
325
326
        // Standard mechanism for accepting permission changes from extensions
327
        $extended = $this->extendedCan('canDelete', $member);
328
        if ($extended !== null) {
329
            return $extended;
330
        }
331
332
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
333
            return false;
334
        }
335
336
        // Default permission check
337
        if (Permission::checkMember($member, ["ADMIN", static::SLIDE_DELETE])) {
338
            return true;
339
        }
340
341
        return false;
342
    }
343
344
    /**
345
     * Stub method to get the site config, unless the current class can provide an alternate.
346
     *
347
     * @return SiteConfig
348
     */
349
    public function getSiteConfig()
350
    {
351
        $configs = $this->invokeWithExtensions('alternateSiteConfig');
352
        foreach (array_filter($configs) as $config) {
353
            return $config;
354
        }
355
356
        return SiteConfig::current_site_config();
357
    }
358
359
    /**
360
     * @return string
361
     */
362
    public function getSlideType()
363
    {
364
        return static::i18n_singular_name();
0 ignored issues
show
Bug Best Practice introduced by
The method SilverStripe\ORM\DataObject::i18n_singular_name() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

364
        return static::/** @scrutinizer ignore-call */ i18n_singular_name();
Loading history...
365
    }
366
}
367