Completed
Push — manage-articles ( 1fa998...1c60ef )
by Fèvre
02:30
created

ArticleValidator::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
namespace Xetaravel\Models\Validators;
3
4
use Eloquence\Behaviours\Slug;
5
use Illuminate\Support\Facades\Validator as FacadeValidator;
6
use Illuminate\Validation\Rule;
7
use Illuminate\Validation\Validator;
8
use Mews\Purifier\Facades\Purifier;
9
10
class ArticleValidator
11
{
12
    /**
13
     * Get a validator for an incoming create request.
14
     *
15
     * @param array $data The data to validate.
16
     *
17
     * @return \Illuminate\Validation\Validator
18
     */
19 View Code Duplication
    public static function create(array $data): Validator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
20
    {
21
        $rules = [
22
            'title' => 'required|min:5',
23
            'category_id' => 'required',
24
            'slug' => 'unique:articles',
25
            'content' => 'required|min:10'
26
        ];
27
28
        if (isset($data['content'])) {
29
            $data['content'] = Purifier::clean($data['content'], 'blog_article');
30
        }
31
        $data['slug'] = Slug::fromTitle($data['title']);
32
33
        return FacadeValidator::make($data, $rules);
34
    }
35
36
    /**
37
     * Get a validator for an incoming update request.
38
     *
39
     * @param array $data The data to validate.
40
     * @param int $id The actual article id to ignore the slug rule.
41
     *
42
     * @return \Illuminate\Validation\Validator
43
     */
44 View Code Duplication
    public static function update(array $data, int $id): Validator
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
45
    {
46
        $rules = [
47
            'title' => 'required|min:5',
48
            'category_id' => 'required',
49
            'slug' => [
50
                Rule::unique('articles')->ignore($id)
51
            ],
52
            'content' => 'required|min:10'
53
        ];
54
55
        if (isset($data['content'])) {
56
            $data['content'] = Purifier::clean($data['content'], 'blog_article');
57
        }
58
        $data['slug'] = Slug::fromTitle($data['title']);
59
60
        return FacadeValidator::make($data, $rules);
61
    }
62
}
63