Completed
Push — 2.0 ( d39b3c...f2914f )
by Kirill
02:52
created

HasValidation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidator() 0 13 1
A afterValidation() 0 10 3
A validate() 0 3 1
A messages() 0 4 1
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\GraphQL\Queries\Support;
10
11
use Illuminate\Validation\Factory;
12
use Folklore\GraphQL\Support\Field;
13
use Illuminate\Contracts\Validation\Validator;
14
15
/**
16
 * Class HasValidation.
17
 * @mixin Field
18
 */
19
trait HasValidation
20
{
21
    /**
22
     * @param $args
23
     * @param $rules
24
     * @return Validator
25
     */
26
    protected function getValidator($args, $rules): Validator
27
    {
28
        /** @var Factory $factory */
29
        $factory = app(Factory::class);
30
31
        $validator = $factory->make($args, $rules, $this->messages());
32
33
        $validator->after(function ($validator) use ($args) {
34
            $this->afterValidation($validator, $args);
35
        });
36
37
        return $validator;
38
    }
39
40
    /**
41
     * @param Validator|\Illuminate\Validation\Validator $validator
42
     * @param array $args
43
     */
44
    private function afterValidation(Validator $validator, array $args): void
45
    {
46
        $validatorErrors = $this->validate($validator, $args);
47
48
        if ($validatorErrors instanceof \Traversable) {
49
            foreach ($validatorErrors as $key => $message) {
50
                $validator->errors()->add($key, $message);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param Validator $validator
57
     * @param array $args
58
     * @return \Generator|null
59
     */
60
    public function validate(Validator $validator, array $args = []): ?\Generator
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function messages(): array
68
    {
69
        return [];
70
    }
71
}
72