Passed
Pull Request — master (#194)
by
unknown
14:54
created

BaseBlogEtcPostRequest::baseBlogPostRules()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 66
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 66
rs 8.5066
c 1
b 0
f 0
cc 7
nc 6
nop 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 WebDevEtc\BlogEtc\Requests;
4
5
abstract class BaseBlogEtcPostRequest extends BaseAdminRequest
6
{
7
    /**
8
     * Shared rules for blog posts.
9
     *
10
     * @todo tidy this up! It is a bit of a mess!
11
     */
12
    protected function baseBlogPostRules(): array
13
    {
14
        // setup some anon functions for some of the validation rules:
15
        // TODO - support sqlite in tests.
16
//        $check_valid_posted_at = function ($attribute, $value, $fail) {
17
//            // just the 'date' validation can cause errors ("2018-01-01 a" passes the validation, but causes a carbon error).
18
//
19
//            try {
20
//                Carbon::createFromFormat('Y-m-d H:i:s', $value);
21
//            } catch (\Exception $e) {
22
//                // return $fail if Carbon could not successfully create a date from $value
23
//                return $fail('Posted at is not a valid date');
24
//            }
25
//        };
26
27
        $show_error_if_has_value = static function ($attribute, $value, $fail) {
28
            if ($value) {
29
                // return $fail if this had a value...
30
                return $fail($attribute.' must be empty');
31
            }
32
        };
33
34
        $disabled_use_view_file = static function ($attribute, $value, $fail) {
35
            if ($value) {
36
                // return $fail if this had a value
37
                return $fail('The use of custom view files is not enabled for this site, so you cannot submit a value for it');
38
            }
39
        };
40
41
        // generate the main set of rules:
42
        $return = [
43
            'posted_at'         => ['nullable', 'date'],
44
            'title'             => ['required', 'string', 'min:1', 'max:255'],
45
            'subtitle'          => ['nullable', 'string', 'min:1', 'max:255'],
46
            'post_body'         => ['required_without:use_view_file', 'max:2000000'], //medium text
47
            'meta_desc'         => ['nullable', 'string', 'min:1', 'max:1000'],
48
            'short_description' => ['nullable', 'string', 'max:30000'],
49
            'slug'              => [
50
                'nullable',
51
                'string',
52
                'min:1',
53
                'max:150',
54
                'alpha_dash', // this field should have some additional rules, which is done in the subclasses.
55
            ],
56
            'category' => ['nullable', 'array'],
57
        ];
58
59
        // is use_custom_view_files true?
60
        if (config('blogetc.use_custom_view_files')) {
61
            $return['use_view_file'] = ['nullable', 'string', 'alpha_num', 'min:1', 'max:75'];
62
        } else {
63
            // use_view_file is disabled, so give an empty if anything is submitted via this function:
64
            $return['use_view_file'] = ['string', $disabled_use_view_file];
65
        }
66
67
        // some additional rules for uploaded images
68
        foreach ((array) config('blogetc.image_sizes') as $size => $image_detail) {
69
            if ($image_detail['enabled'] && config('blogetc.image_upload_enabled')) {
70
                $return[$size] = ['nullable', 'image'];
71
            } else {
72
                // was not enabled (or all images are disabled), so show an error if it was submitted:
73
                $return[$size] = $show_error_if_has_value;
74
            }
75
        }
76
77
        return $return;
78
    }
79
}
80