Completed
Pull Request — 1.0 (#108)
by Nic
06:25
created

SlideImage::canView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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
            $link->setTitle("'Choose a page to link to:'");
95 1
        }
96
97 1
        if($image = $fields->dataFieldByName('Image')){
98 1
            $image->setFolderName('Uploads/SlideImages')
99 1
                ->setAllowedMaxFileNumber(1)
100 1
                ->setAllowedFileCategories('image');
101 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...
102 1
        }
103
104 1
        $fields->dataFieldByName('ShowSlide')
105 1
            ->setDescription('Include this slide in the slider. Uncheck to hide');
106
107 1
        return $fields;
108
    }
109
110
    /**
111
     * @return ValidationResult
112
     */
113 6
    public function validate()
114
    {
115 6
        $result = parent::validate();
116
117 6
        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...
118 1
            $result->error('A Name is required before you can save');
119 1
        }
120
121 6
        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...
122 2
            $result->error('An Image is required before you can save');
123 2
        }
124
125 6
        return $result;
126
    }
127
128
    /**
129
     * @return array
130
     */
131 1
    public function providePermissions()
132
    {
133
        return array(
134 1
            'Slide_EDIT' => 'Slide Edit',
135 1
            'Slide_DELETE' => 'Slide Delete',
136 1
            'Slide_CREATE' => 'Slide Create',
137 1
        );
138
    }
139
140
    /**
141
     * @param null $member
142
     * @return bool|int
143
     */
144 1
    public function canCreate($member = null)
145
    {
146 1
        return Permission::check('Slide_CREATE');
147
    }
148
149
    /**
150
     * @param null $member
151
     * @return bool|int
152
     */
153 1
    public function canEdit($member = null)
154
    {
155 1
        return Permission::check('Slide_EDIT');
156
    }
157
158
    /**
159
     * @param null $member
160
     * @return bool|int
161
     */
162 1
    public function canDelete($member = null)
163
    {
164 1
        return Permission::check('Slide_DELETE');
165
    }
166
167
    /**
168
     * @param null $member
169
     * @return bool
170
     */
171 1
    public function canView($member = null)
172
    {
173 1
        return true;
174
    }
175
}
176