Completed
Pull Request — master (#6)
by ARCANEDEV
04:27
created

PostRequest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 7
dl 0
loc 106
rs 10
c 0
b 0
f 0
ccs 0
cts 45
cp 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 15 1
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
     * Get the validated inputs.
50
     *
51
     * @return array
52
     */
53
    public function getValidatedData()
54
    {
55
        return array_merge([
56
            'author_id'   => $this->user()->getAuthIdentifier(),
57
            'category_id' => $this->get('category')
58
        ], $this->only([
59
            // POST inputs
60
            'locale', 'title', 'excerpt', 'thumbnail', 'content', 'tags', 'published_at', 'status',
61
62
            // SEO inputs
63
            'seo_title', 'seo_description', 'seo_keywords',
64
        ]));
65
    }
66
67
    /**
68
     * Get the slug rule.
69
     *
70
     * @param  string  $column
71
     *
72
     * @return \Illuminate\Validation\Rules\Unique
73
     */
74
    protected static function getSlugRule($column = 'slug')
75
    {
76
        $prefix = config('arcanesoft.blog.database.prefix', 'blog_');
77
78
        return Rule::unique("{$prefix}posts", $column);
79
    }
80
81
    /**
82
     * Get the category rule.
83
     *
84
     * @return array
85
     */
86
    protected static function getCategoryRule()
87
    {
88
        return ['required', 'integer', 'in:'.Category::getSelectOptions(false)->keys()->implode(',')];
89
    }
90
91
    /**
92
     * Get the tags rule.
93
     *
94
     * @return array
95
     */
96
    protected static function getTagsRule()
97
    {
98
        return ['required', 'array', 'min:1', 'in:'.Tag::getSelectOptions()->keys()->implode(',')];
99
    }
100
101
    /**
102
     * Get the post status rule.
103
     *
104
     * @return array
105
     */
106
    protected static function getPostStatusRule()
107
    {
108
        return ['required', 'string', 'in:'.Post::getStatuses()->keys()->implode(',')];
109
    }
110
111
    /**
112
     * Get the post local rule.
113
     *
114
     * @return array
115
     */
116
    protected function getLocaleRule()
117
    {
118
        return ['required', 'string', 'in:'.implode(',', $this->getSupportedLocales())];
119
    }
120
}
121