Completed
Pull Request — master (#45)
by Fèvre
32:37
created

DiscussCategoryValidator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 45
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 12 12 1
A update() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
9 View Code Duplication
class DiscussCategoryValidator
0 ignored issues
show
Duplication introduced by
This class 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...
10
{
11
    /**
12
     * Get a validator for an incoming create request.
13
     *
14
     * @param array $data The data to validate.
15
     *
16
     * @return \Illuminate\Validation\Validator
17
     */
18
    public static function create(array $data): Validator
19
    {
20
        $rules = [
21
            'title' => 'required|min:5',
22
            'slug' => 'unique:discuss_categories',
23
            'color' => 'min:7|max:7',
24
            'description' => 'required|min:10'
25
        ];
26
        $data['slug'] = Slug::fromTitle($data['title']);
27
28
        return FacadeValidator::make($data, $rules);
29
    }
30
31
    /**
32
     * Get a validator for an incoming update request.
33
     *
34
     * @param array $data The data to validate.
35
     * @param int $id The actual category id to ignore the slug rule.
36
     *
37
     * @return \Illuminate\Validation\Validator
38
     */
39
    public static function update(array $data, int $id): Validator
40
    {
41
        $rules = [
42
            'title' => 'required|min:5',
43
            'slug' => [
44
                Rule::unique('discuss_categories')->ignore($id)
45
            ],
46
            'color' => 'min:7|max:7',
47
            'description' => 'required|min:10'
48
        ];
49
        $data['slug'] = Slug::fromTitle($data['title']);
50
51
        return FacadeValidator::make($data, $rules);
52
    }
53
}
54