Completed
Pull Request — master (#115)
by Jason
04:23
created

SlideImage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 5
dl 0
loc 168
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 32 1
A validate() 0 14 3
A providePermissions() 0 8 1
A canCreate() 0 4 1
A canEdit() 0 4 1
A canDelete() 0 4 1
A canView() 0 4 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 $summary_fields = array(
44
        'Image.CMSThumbnail' => 'Image',
45
        'Name' => 'Name',
46
    );
47
48
    /**
49
     * @var array
50
     */
51
    private static $searchable_fields = array(
52
        'Name',
53
        'Headline',
54
        'Description',
55
    );
56
57
    /**
58
     * @var int
59
     */
60
    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...
61
62
    /**
63
     * @var array
64
     */
65
    private static $extensions = [
66
        'VersionedDataObject',
67
    ];
68
69
    /**
70
     * @return FieldList
71
     */
72
    public function getCMSFields()
73
    {
74
        $fields = parent::getCMSFields();
75
76
        $fields->removeByName([
77
            'ShowSlide',
78
            'SortOrder',
79
            'PageID',
80
        ]);
81
82
        $fields->dataFieldByName('Name')
83
            ->setDescription('for internal reference only');
84
85
        $fields->dataFieldByName('Headline')
86
            ->setDescription('optional, used in template');
87
88
        $fields->dataFieldByName('Description')
89
            ->setDescription('optional, used in template');
90
91
        $fields->dataFieldByName('PageLinkID')
92
            ->setTitle("Choose a page to link to:");
93
94
        $image = $fields->dataFieldByName('Image')
95
            ->setFolderName('Uploads/SlideImages')
96
            ->setAllowedMaxFileNumber(1)
97
            ->setAllowedFileCategories('image');
98
        $fields->insertAfter($image, 'Description');
0 ignored issues
show
Documentation introduced by
'Description' 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...
99
100
        $this->extend('updateSlideImageFields', $fields);
101
102
        return $fields;
103
    }
104
105
    /**
106
     * @return ValidationResult
107
     */
108
    public function validate()
109
    {
110
        $result = parent::validate();
111
112
        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...
113
            $result->error('A Name is required before you can save');
114
        }
115
116
        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...
117
            $result->error('An Image is required before you can save');
118
        }
119
120
        return $result;
121
    }
122
123
    /**
124
     * @return array
125
     */
126
    public function providePermissions()
127
    {
128
        return array(
129
            'Slide_EDIT' => 'Slide Edit',
130
            'Slide_DELETE' => 'Slide Delete',
131
            'Slide_CREATE' => 'Slide Create',
132
        );
133
    }
134
135
    /**
136
     * @param null $member
137
     * @return bool|int
138
     */
139
    public function canCreate($member = null)
140
    {
141
        return Permission::check('Slide_CREATE', 'any', $member);
142
    }
143
144
    /**
145
     * @param null $member
146
     * @return bool|int
147
     */
148
    public function canEdit($member = null)
149
    {
150
        return Permission::check('Slide_EDIT', 'any', $member);
151
    }
152
153
    /**
154
     * @param null $member
155
     * @return bool|int
156
     */
157
    public function canDelete($member = null)
158
    {
159
        return Permission::check('Slide_DELETE', 'any', $member);
160
    }
161
162
    /**
163
     * @param null $member
164
     * @return bool
165
     */
166
    public function canView($member = null)
167
    {
168
        return true;
169
    }
170
}
171