Passed
Pull Request — master (#11)
by Jason
03:37
created

Job::getCMSFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 44
nc 1
nop 0
dl 0
loc 65
ccs 43
cts 43
cp 1
crap 2
rs 9.216
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
namespace Dynamic\Jobs\Page;
4
5
use Dynamic\Jobs\Model\JobCategory;
6
use Dynamic\Jobs\Model\JobSection;
7
use Dynamic\Jobs\Model\JobSubmission;
8
use \Page;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Forms\DateField;
11
use SilverStripe\Forms\DropdownField;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\GridField\GridField;
14
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
15
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\ListboxField;
18
use SilverStripe\Forms\TreeMultiselectField;
19
use SilverStripe\ORM\Search\SearchContext;
20
use SilverStripe\Security\Permission;
21
use SilverStripe\Security\PermissionProvider;
22
use Symbiote\GridFieldExtensions\GridFieldAddExistingSearchButton;
23
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
24
25
/**
26
 * Class Job
27
 * @package Dynamic\Jobs\Model
28
 */
29
class Job extends Page implements PermissionProvider
30
{
31
    /**
32
     * @var string
33
     */
34
    private static $singular_name = 'Job';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     * @var string
38
     */
39
    private static $plural_name = 'Jobs';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
40
41
    /**
42
     * @var string
43
     */
44
    private static $description = 'Job detail page allowing for application submissions';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
45
46
    /**
47
     * @var string
48
     */
49
    private static $table_name = 'Dynamic_Job';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var array
53
     */
54
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
55
        'PositionType' => "Enum(array('Full-time', 'Part-time', 'Freelance', 'Internship'))",
56
        'PostDate' => 'Date',
57
        'EndPostDate' => 'Date',
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
64
        'Sections' => JobSection::class,
65
        'Submissions' => JobSubmission::class,
66
    ];
67
68
    /**
69
     * @var array
70
     */
71
    private static $many_many = [
0 ignored issues
show
introduced by
The private property $many_many is not used, and could be removed.
Loading history...
72
        'Categories' => JobCategory::class,
73
    ];
74
75
    /**
76
     * @var array
77
     */
78
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
79
        'Categories' => [
80
            'Sort' => 'Int',
81
        ],
82
    ];
83
84
    /**
85
     * @var array
86
     */
87
    private static $searchable_fields = [
0 ignored issues
show
introduced by
The private property $searchable_fields is not used, and could be removed.
Loading history...
88
        'Title',
89
        'Categories.ID' => [
90
            'title' => 'Category',
91
        ],
92
        'PositionType' => [
93
            'title' => 'Type',
94
        ],
95
    ];
96
97
    /**
98
     * @var string
99
     */
100
    private static $default_parent = JobCollection::class;
0 ignored issues
show
introduced by
The private property $default_parent is not used, and could be removed.
Loading history...
101
102
    /**
103
     * @var bool
104
     */
105
    private static $can_be_root = false;
0 ignored issues
show
introduced by
The private property $can_be_root is not used, and could be removed.
Loading history...
106
107
    /**
108
     * @var bool
109
     */
110
    private static $show_in_sitetree = false;
0 ignored issues
show
introduced by
The private property $show_in_sitetree is not used, and could be removed.
Loading history...
111
112
    /**
113
     * @var array
114
     */
115
    private static $allowed_children = [];
0 ignored issues
show
introduced by
The private property $allowed_children is not used, and could be removed.
Loading history...
116
117
    /**
118
     * @return SearchContext
119
     */
120
    public function getCustomSearchContext()
121
    {
122
        $fields = $this->scaffoldSearchFields([
123
            'restrictFields' => [
124
                'Title',
125
                'Categories.ID',
126
                'PositionType',
127
            ],
128
        ]);
129
130
        $filters = $this->defaultSearchFilters();
131
132
        return new SearchContext(
133
            $this->ClassName,
134
            $fields,
135
            $filters
136
        );
137
    }
138
139
    /**
140
     *
141
     */
142 14
    public function populateDefaults()
143
    {
144 14
        $this->PostDate = date('Y-m-d');
0 ignored issues
show
Bug Best Practice introduced by
The property PostDate does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
145
146 14
        parent::populateDefaults();
147
    }
148
149
    /**
150
     * @return FieldList
151
     */
152
    public function getCMSFields()
153
    {
154 1
        $this->beforeUpdateCMSFields(function ($fields) {
155 1
            $fields->dataFieldByName('Title')
156 1
                ->setTitle('Position Name');
157
158 1
            $fields->dataFieldByName('Content')
159 1
                ->setRows(10)
160 1
                ->setTitle('Introduction');
161
162 1
            $fields->addFieldsToTab('Root.Details', [
163 1
                DropdownField::create(
164 1
                    'PositionType',
165 1
                    'Position Type',
166 1
                    Job::singleton()->dbObject('PositionType')->enumValues()
167 1
                )->setEmptyString('--select--'),
168 1
                DateField::create('PostDate', 'Post Start Date')
169 1
                    ->setDescription('Date position should appear on website'),
170 1
                DateField::create('EndPostDate', 'Post End Date')
171 1
                    ->setDescription('Date position should be removed from website'),
172
            ]);
173
174 1
            if ($this->ID) {
175
                // categories
176 1
                $categoriesField = ListboxField::create(
177 1
                    'Categories',
178 1
                    'Categories',
179 1
                    JobCategory::get()->map()
180
                );
181
182 1
                $fields->addFieldsToTab('Root.Details', [
183 1
                    $categoriesField,
184
                ]);
185
186
                // sections
187 1
                $config = GridFieldConfig_RelationEditor::create();
188
                $config
189 1
                    ->addComponent(new GridFieldOrderableRows('Sort'))
190 1
                    ->addComponent(new GridFieldDeleteAction(false))
191 1
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
192 1
                    ->removeComponentsByType(GridFieldDeleteAction::class);
193
194 1
                $sections = $this->Sections()->sort('Sort');
0 ignored issues
show
Bug introduced by
The method Sections() does not exist on Dynamic\Jobs\Page\Job. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
                $sections = $this->/** @scrutinizer ignore-call */ Sections()->sort('Sort');
Loading history...
195 1
                $sectionsField = GridField::create('Sections', 'Sections', $sections, $config)
196 1
                    ->setDescription('ex: Requirements, Education, Duties, etc.');
197 1
                $fields->addFieldsToTab('Root.Main', [
198 1
                    $sectionsField,
199
                ]);
200
201
                // submissions
202 1
                $config = GridFieldConfig_RelationEditor::create();
203
                $config
204 1
                    ->addComponent(new GridFieldDeleteAction(false))
205 1
                    ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
206 1
                    ->removeComponentsByType(GridFieldDeleteAction::class);
207
208 1
                $submissions = $this->Submissions();
0 ignored issues
show
Bug introduced by
The method Submissions() does not exist on Dynamic\Jobs\Page\Job. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

208
                /** @scrutinizer ignore-call */ 
209
                $submissions = $this->Submissions();
Loading history...
209 1
                $submissionsField = GridField::create('Submissions', 'Submissions', $submissions, $config);
210 1
                $fields->addFieldsToTab('Root.Submissions', [
211 1
                    $submissionsField,
212
                ]);
213
            }
214 1
        });
215
216 1
        return parent::getCMSFields();
217
    }
218
219
    /**
220
     * @return string
221
     */
222 1
    public function getApplyButton()
223
    {
224 1
        $apply = Controller::join_links(
225 1
            $this->Link(),
226 1
            'apply'
227
        );
228 1
        return $apply;
229
    }
230
231
    /**
232
     * @return mixed
233
     */
234 1
    public function getApplicationLink()
235
    {
236 1
        if ($this->parent()->Application()->ID != 0) {
237
            return $this->parent()->Application()->URL;
238
        }
239 1
        return false;
240
    }
241
242
    /**
243
     * @return mixed
244
     */
245
    public function getCategoryList()
246
    {
247
        return $this->Categories()->sort('Sort');
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\Jobs\Page\Job. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

247
        return $this->/** @scrutinizer ignore-call */ Categories()->sort('Sort');
Loading history...
248
    }
249
250
    /**
251
     * @return bool
252
     */
253
    public function getPrimaryCategory()
254
    {
255
        if ($this->Categories()->exists()) {
256
            return $this->Categories()->first();
257
        }
258
        return false;
259
    }
260
261
    /**
262
     * @return array
263
     */
264 1
    public function providePermissions()
265
    {
266
        return [
267 1
            'Job_EDIT' => 'Edit a Job',
268
            'Job_DELETE' => 'Delete a Job',
269
            'Job_CREATE' => 'Create a Job',
270
        ];
271
    }
272
273
    /**
274
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
275
     *
276
     * @return bool|int
277
     */
278 1
    public function canEdit($member = null)
279
    {
280 1
        return Permission::check('Job_EDIT', 'any', $member);
281
    }
282
283
    /**
284
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
285
     *
286
     * @return bool|int
287
     */
288 1
    public function canDelete($member = null)
289
    {
290 1
        return Permission::check('Job_DELETE', 'any', $member);
291
    }
292
293
    /**
294
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
295
     *
296
     * @return bool|int
297
     */
298 1
    public function canCreate($member = null, $context = [])
299
    {
300 1
        return Permission::check('Job_CREATE', 'any', $member);
301
    }
302
303
    /**
304
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
305
     *
306
     * @return bool
307
     */
308 4
    public function canView($member = null)
309
    {
310 4
        return true;
311
    }
312
}
313