GenericPage::getCMSFields()   B
last analyzed

Complexity

Conditions 8
Paths 96

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 96
nop 0
dl 0
loc 69
rs 7.4319
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * The core of HandyPages is this generic page - contains all logic and smarts.
5
 *
6
 * @author Peter Thaleikis
7
 */
8
9
class GenericPage extends Page
10
{
11
    /**
12
     * @var array
13
     */
14
    private static $db = [
15
        'AlternativeTitle' => 'varchar(50)',
16
        'Intro' => 'varchar(1000)',
17
        'Color' => 'varchar(50)',
18
    ];
19
20
    /**
21
     * @var array
22
     */
23
    private static $has_one = [
24
        'Image' => 'Image',
25
    ];
26
27
    /**
28
     * @return FieldList
29
     */
30
    public function getCMSFields()
31
    {
32
        $fields = parent::getCMSFields();
33
34
        $hiddenFields = $this->config()->get('hideCMSInputs');
35
36
        // short title?
37
        if (!in_array('AlternativeTitle', $hiddenFields)) {
38
            $fields->addFieldToTab(
39
                'Root.Main',
40
                TextField::create('AlternativeTitle', 'Alternative Title'),
41
                'URLSegment'
42
            );
43
        }
44
45
        // do we want an intro?
46
        if (!in_array('Intro', $hiddenFields)) {
47
            $fields->addFieldToTab(
48
                'Root.Main',
49
                TextareaField::create('Intro', 'Intro'),
50
                'Content'
51
            );
52
        }
53
54
        // add an image
55
        if (!in_array('Image', $hiddenFields)) {
56
            $fields->addFieldToTab(
57
                'Root.Main',
58
                UploadField::create('ImageID', 'Image')
59
                    ->setAllowedFileCategories('image')
60
                    ->setAllowedMaxFileNumber(1),
61
                'Content'
62
            );
63
        }
64
65
        // do we use a section color?
66
        if (!in_array('Color', $hiddenFields)) {
67
            // default is this brand color.
68
            $field = NoticeMessage::create('The brand colors have not been defined yet.');
69
70
            // usually we replace this with the color palette.
71
            if (BrandColors::get()->count() > 0) {
72
                $field = ColorPaletteField::create(
73
                    'Color',
74
                    'Color',
75
                    BrandColors::get()->map('BrandColor', 'ColorName')
76
                )->setRightTitle('Please select a brand color.');
77
            }
78
79
            $fields->addFieldToTab('Root.Main', $field, 'Content');
80
        }
81
82
        // It will repalce Content area field to notice message field, or nothing at the end.
83
        if ($this->config()->get('useBlocksModule')) {
84
            // move the block related fields over
85
            $fields->addFieldsToTab('Root.Main', $fields->findOrMakeTab('Root.Blocks')->Fields(), 'Content');
86
87
            // now we really don't need the content field anymore.
88
            $fields->removeByName('Content');
89
90
            // remove the blocks tab
91
            $fields->removeByName('Root.Blocks');
92
        }
93
        if ($this->config()->get('hideContentField')) {
94
            $fields->removebyName('Content');
95
        }
96
97
        return $fields;
98
    }
99
100
    /**
101
     * @return ZenValidator
102
     */
103
    public function getCMSValidator()
104
    {
105
        // create any validation contrain we need
106
        $validator = ZenValidator::create();
107
108
        foreach ($this->config()->get('validation') as $field => $validation) {
109
            // protection against misconfiguration: is this field actually shown? no? okay, don't validate.
110
            if (in_array($field, $this->config()->hideCMSInputs)) {
111
                // just required?
112
                if (array_key_exists('required', $validation)) {
113
                    $validator->setConstraint(
114
                        $field,
115
                        Constraint_require::create()->setMessage('This field is required.')
116
                    );
117
                }
118
119
                // min length?
120 View Code Duplication
                if (array_key_exists('minLength', $validation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
                    $validator->setConstraint(
122
                        $field,
123
                        Constraint_length::create('min', $validation['minLength'])
124
                            ->setMessage(sprintf('Please enter at least %d characters.', $validation['minLength']))
125
                    );
126
                }
127
128
                // max length?
129 View Code Duplication
                if (array_key_exists('maxLength', $validation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
                    $validator->setConstraint(
131
                        $field,
132
                        Constraint_length::create('max', $validation['maxLength'])
133
                            ->setMessage(sprintf('Please enter maximal %d characters.', $validation['maxLength']))
134
                    );
135
                }
136
137
                // width?
138 View Code Duplication
                if (array_key_exists('width', $validation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
                    $validator->setConstraint(
140
                        $field,
141
                        Constraint_dimension::create('width', $validation['width'])
142
                            ->setMessage(sprintf(
143
                                'Please upload an image with an width of %s pixel.',
144
                                $validation['width']
145
                            )
146
                        )
147
                    );
148
                }
149
150
                // height?
151 View Code Duplication
                if (array_key_exists('height', $validation)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
                    $validator->setConstraint(
153
                        $field,
154
                        Constraint_dimension::create('height', $validation['height'])
155
                            ->setMessage(sprintf(
156
                                'Please upload an image with an height of %s pixel.',
157
                                $validation['height']
158
                            )
159
                        )
160
                    );
161
                }
162
            }
163
        }
164
165
        $this->extend('updateCMSValidator', $validator);
166
167
        return $validator;
168
    }
169
}
170
171
class GenericPage_Controller extends Page_Controller
172
{
173
}
174