Completed
Pull Request — 1.0 (#131)
by Nic
02:01
created

SlideImage::canEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
class SlideImage extends DataObject implements PermissionProvider
4
{
5
    /**
6
     * @var string
7
     */
8
    private static $singular_name = 'Slide';
9
10
    /**
11
     * @var string
12
     */
13
    private static $plural_name = 'Slides';
14
15
    /**
16
     * @var array
17
     */
18
    private static $db = array(
19
        'Name' => 'Varchar(255)',
20
        'Headline' => 'Varchar(255)',
21
        'Description' => 'Text',
22
        'SortOrder' => 'Int',
23
        'ShowSlide' => 'Boolean',
24
    );
25
26
    /**
27
     * @var array
28
     */
29
    private static $has_one = array(
30
        'Image' => 'Image',
31
        'Page' => 'Page',
32
        'PageLink' => 'SiteTree',
33
    );
34
35
    /**
36
     * @var string
37
     */
38
    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...
39
40
    /**
41
     * @var array
42
     */
43
    private static $defaults = array(
44
        'ShowSlide' => true,
45
    );
46
47
    /**
48
     * @var array
49
     */
50
    private static $summary_fields = array(
51
        'Image.CMSThumbnail' => 'Image',
52
        'Name' => 'Name',
53
    );
54
55
    /**
56
     * @var array
57
     */
58
    private static $searchable_fields = array(
59
        'Name',
60
        'Headline',
61
        'Description',
62
    );
63
64
    /**
65
     * @var int
66
     */
67
    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...
68
69
    /**
70
     * @return FieldList
71
     */
72 1
    public function getCMSFields()
73
    {
74 1
        $fields = parent::getCMSFields();
75
76 1
        $fields->removeByName([
77 1
            'SortOrder',
78 1
            'PageID',
79 1
        ]);
80
81 1
        if($name = $fields->dataFieldByName('Name')){
82 1
            $name->setDescription('for internal reference only');
83 1
        }
84
85 1
        if($headline = $fields->dataFieldByName('Headline')){
86 1
            $headline->setDescription('optional, used in template');
87 1
        }
88
89 1
        if($description = $fields->dataFieldByName('Description')){
90 1
            $description->setDescription('optional, used in template');
91 1
        }
92
93 1
        if($link = $fields->dataFieldByName('PageLinkID')){
94 1
            if(!$link instanceof TreeDropdownField){
95 1
                $fields->replaceField(
96
                    'PageLinkID',
97 1
                    $link = TreeDropdownField::create('PageLinkID', null, SiteTree::class)
98 1
                );
99 1
            }
100 1
            $link->setTitle("Choose a page to link to:");
101 1
        }
102 1
103
        if($image = $fields->dataFieldByName('Image')){
104 1
            $image->setFolderName('Uploads/SlideImages')
105 1
                ->setAllowedMaxFileNumber(1)
106
                ->setAllowedFileCategories('image');
107 1
            $fields->insertBefore($image, 'ShowSlide');
0 ignored issues
show
Documentation introduced by
'ShowSlide' is of type string, but the function expects a object<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...
108
        }
109
110
        $fields->dataFieldByName('ShowSlide')
111
            ->setDescription('Include this slide in the slider. Uncheck to hide');
112
113 6
        return $fields;
114
    }
115 6
116
    /**
117 6
     * @return ValidationResult
118 1
     */
119 1
    public function validate()
120
    {
121 6
        $result = parent::validate();
122 2
123 2
        if (!$this->Name) {
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<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...
124
            $result->error('A Name is required before you can save');
125 6
        }
126
127
        if (!$this->ImageID) {
0 ignored issues
show
Documentation introduced by
The property ImageID does not exist on object<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...
128
            $result->error('An Image is required before you can save');
129
        }
130
131 1
        return $result;
132
    }
133
134 1
    /**
135 1
     * @return array
136 1
     */
137 1
    public function providePermissions()
138
    {
139
        return array(
140
            'Slide_EDIT' => 'Slide Edit',
141
            'Slide_DELETE' => 'Slide Delete',
142
            'Slide_CREATE' => 'Slide Create',
143
        );
144 1
    }
145
146 1
    /**
147
     * @param null $member
148
     * @return bool|int
149
     */
150
    public function canCreate($member = null)
151
    {
152
        return Permission::check('Slide_CREATE');
153 1
    }
154
155 1
    /**
156
     * @param null $member
157
     * @return bool|int
158
     */
159
    public function canEdit($member = null)
160
    {
161
        return Permission::check('Slide_EDIT');
162 1
    }
163
164 1
    /**
165
     * @param null $member
166
     * @return bool|int
167
     */
168
    public function canDelete($member = null)
169
    {
170
        return Permission::check('Slide_DELETE');
171 1
    }
172
173 1
    /**
174
     * @param null $member
175
     * @return bool
176 1
     */
177
    public function canView($member = null)
178
    {
179
        return true;
180
    }
181
}
182