PostRequest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 0
loc 118
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 15 1
A prepareForValidation() 0 8 2
A getValidatedData() 0 13 1
A getSlugRule() 0 6 1
A getCategoryRule() 0 4 1
A getTagsRule() 0 4 1
A getPostStatusRule() 0 4 1
A getLocaleRule() 0 4 1
1
<?php namespace Arcanesoft\Blog\Http\Requests\Admin\Posts;
2
3
use Arcanesoft\Blog\Http\Requests\Admin\FormRequest;
4
use Arcanesoft\Blog\Models\Category;
5
use Arcanesoft\Blog\Models\Post;
6
use Arcanesoft\Blog\Models\Tag;
7
use Illuminate\Validation\Rule;
8
9
/**
10
 * Class     PostRequest
11
 *
12
 * @package  Arcanesoft\Blog\Http\Requests\Admin\Posts
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class PostRequest extends FormRequest
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array
26
     */
27
    public function rules()
28
    {
29
        // TODO: Adding seo rules
30
        return [
31
            'locale'       => $this->getLocaleRule(),
32
            'title'        => ['required', 'string', 'max:255'],
33
            'excerpt'      => ['required', 'string', 'max:200'],
34
            'thumbnail'    => ['nullable', 'string', 'url'],
35
            'content'      => ['required', 'string'],
36
            'category'     => static::getCategoryRule(),
37
            'tags'         => static::getTagsRule(),
38
            'published_at' => ['required', 'date_format:Y-m-d'],
39
            'status'       => static::getPostStatusRule(),
40
        ];
41
    }
42
43
    /* -----------------------------------------------------------------
44
     |  Other Methods
45
     | -----------------------------------------------------------------
46
     */
47
48
    /**
49
     * Prepare the data for validation.
50
     */
51
    protected function prepareForValidation()
52
    {
53
        parent::prepareForValidation();
54
55
        if ( ! $this->isTranslatable()) {
56
            $this->merge(['locale' => config('app.locale')]);
57
        }
58
    }
59
60
    /**
61
     * Get the validated inputs.
62
     *
63
     * @return array
64
     */
65
    public function getValidatedData()
66
    {
67
        return array_merge([
68
            'author_id'   => $this->user()->getAuthIdentifier(),
69
            'category_id' => $this->get('category')
70
        ], $this->only([
71
            // POST inputs
72
            'locale', 'title', 'slug', 'excerpt', 'thumbnail', 'content', 'tags', 'published_at', 'status',
73
74
            // SEO inputs
75
            'seo_title', 'seo_description', 'seo_keywords',
76
        ]));
77
    }
78
79
    /**
80
     * Get the slug rule.
81
     *
82
     * @param  string  $column
83
     *
84
     * @return \Illuminate\Validation\Rules\Unique
85
     */
86
    protected static function getSlugRule($column = 'slug')
87
    {
88
        $prefix = config('arcanesoft.blog.database.prefix', 'blog_');
89
90
        return Rule::unique("{$prefix}posts", $column);
91
    }
92
93
    /**
94
     * Get the category rule.
95
     *
96
     * @return array
97
     */
98
    protected static function getCategoryRule()
99
    {
100
        return ['required', 'integer', 'in:'.Category::getSelectData(false)->keys()->implode(',')];
101
    }
102
103
    /**
104
     * Get the tags rule.
105
     *
106
     * @return array
107
     */
108
    protected static function getTagsRule()
109
    {
110
        return ['required', 'array', 'min:1', 'in:'.Tag::getSelectData()->keys()->implode(',')];
111
    }
112
113
    /**
114
     * Get the post status rule.
115
     *
116
     * @return array
117
     */
118
    protected static function getPostStatusRule()
119
    {
120
        return ['required', 'string', 'in:'.Post::getStatuses()->keys()->implode(',')];
121
    }
122
123
    /**
124
     * Get the post local rule.
125
     *
126
     * @return array
127
     */
128
    protected function getLocaleRule()
129
    {
130
        return ['required', 'string', 'in:'.implode(',', $this->getSupportedLocales())];
131
    }
132
}
133