Completed
Pull Request — master (#109)
by Jason
06:12
created

SlideImage::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Dynamic\FlexSlider;
4
5
use SilverStripe\Assets\Image;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Permission;
10
use SilverStripe\Security\PermissionProvider;
11
use SilverStripe\Versioned\Versioned;
12
13
class SlideImage extends DataObject implements PermissionProvider
14
{
15
    /**
16
     * @var string
17
     */
18
    private static $singular_name = 'Slide';
19
20
    /**
21
     * @var string
22
     */
23
    private static $plural_name = 'Slides';
24
25
    /**
26
     * @var array
27
     */
28
    private static $db = array(
29
        'Name' => 'Varchar(255)',
30
        'Headline' => 'Varchar(255)',
31
        'Description' => 'Text',
32
        'SortOrder' => 'Int',
33
        'ShowSlide' => 'Boolean',
34
    );
35
36
    /**
37
     * @var array
38
     */
39
    private static $has_one = array(
40
        'Image' => Image::class,
41
        'Page' => \Page::class,
42
        'PageLink' => SiteTree::class,
43
    );
44
45
    /**
46
     * @var string
47
     */
48
    private static $default_sort = 'SortOrder';
0 ignored issues
show
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
    /**
51
     * @var array
52
     */
53
    private static $summary_fields = array(
54
        'Image.CMSThumbnail' => 'Image',
55
        'Name' => 'Name',
56
    );
57
58
    /**
59
     * @var array
60
     */
61
    private static $searchable_fields = array(
62
        'Name',
63
        'Headline',
64
        'Description',
65
    );
66
67
    /**
68
     * @var int
69
     */
70
    private static $image_size_limit = 512000;
0 ignored issues
show
Unused Code introduced by
The property $image_size_limit is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
71
72
    /**
73
     * @return FieldList
74
     */
75 1
    public function getCMSFields()
76
    {
77 1
        $fields = parent::getCMSFields();
78
79 1
        $fields->removeByName([
80 1
            'ShowSlide',
81
            'SortOrder',
82
            'PageID',
83
        ]);
84
85 1
        $fields->dataFieldByName('Name')
86 1
            ->setDescription('for internal reference only');
87
88 1
        $fields->dataFieldByName('Headline')
89 1
            ->setDescription('optional, used in template');
90
91 1
        $fields->dataFieldByName('Description')
92 1
            ->setDescription('optional, used in template');
93
94 1
        $fields->dataFieldByName('PageLinkID')
95 1
            ->setTitle("Choose a page to link to:");
96
97 1
        $image = $fields->dataFieldByName('Image')
98
            //->setFolderName('Uploads/SlideImages')
99
            //->setAllowedMaxFileNumber(1)
100
            //->setAllowedFileCategories('image')
101
        ;
102 1
        $fields->insertAfter($image, 'Description');
0 ignored issues
show
Documentation introduced by
$image is of type object<SilverStripe\Forms\FormField>|null, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
'Description' is of type string, but the function expects a object<SilverStripe\Forms\FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
104 1
        $this->extend('updateSlideImageFields', $fields);
105
106 1
        return $fields;
107
    }
108
109
    /**
110
     * @return \SilverStripe\ORM\ValidationResult
111
     */
112 3
    public function validate()
113
    {
114 3
        $result = parent::validate();
115
116 3
        if (!$this->Name) {
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Dynamic\FlexSlider\SlideImage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117 1
            $result->addError('A Name is required before you can save');
118
        }
119
120 3
        if (!$this->ImageID) {
0 ignored issues
show
Documentation introduced by
The property ImageID does not exist on object<Dynamic\FlexSlider\SlideImage>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121 2
            $result->addError('An Image is required before you can save');
122
        }
123
124
        Member::class;
125
126 3
        return $result;
127
    }
128
129
    /**
130
     * @return array
131
     */
132 1
    public function providePermissions()
133
    {
134
        return array(
135 1
            'Slide_EDIT' => 'Slide Edit',
136
            'Slide_DELETE' => 'Slide Delete',
137
            'Slide_CREATE' => 'Slide Create',
138
        );
139
    }
140
141
    /**
142
     * @param null $member
143
     * @param array $context
144
     * @return bool|int
145
     */
146 1
    public function canCreate($member = null, $context = [])
147
    {
148 1
        return Permission::check('Slide_CREATE', 'any', $member);
149
    }
150
151
    /**
152
     * @param null $member
153
     * @param array $context
154
     * @return bool|int
155
     */
156 1
    public function canEdit($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
    {
158 1
        return Permission::check('Slide_EDIT', 'any', $member);
159
    }
160
161
    /**
162
     * @param null $member
163
     * @param array $context
164
     * @return bool|int
165
     */
166 1
    public function canDelete($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168 1
        return Permission::check('Slide_DELETE', 'any', $member);
169
    }
170
171
    /**
172
     * @param null $member
173
     * @param array $context
174
     * @return bool
175
     */
176 1
    public function canView($member = null, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
    {
178 1
        return true;
179
    }
180
}
181