ValidateTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 11
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getValidatorFactory() 0 3 1
A validate() 0 12 2
1
<?php
2
3
namespace Napp\Core\Api\Validation;
4
5
use Illuminate\Contracts\Validation\Factory as ValidatorFactory;
6
use Illuminate\Contracts\Validation\Validator;
7
use Napp\Core\Api\Exceptions\Exceptions\ValidationException;
8
9
/**
10
 * Trait ValidateTrait.
11
 */
12
trait ValidateTrait
13
{
14
    /**
15
     * @param array $attributes
16
     * @param array $rules
17
     *
18
     * @throws ValidationException
19
     *
20
     * @return void
21
     */
22
    public static function validate(array $attributes, array $rules)
23
    {
24
        /** @var Validator $validator */
25
        $validator = static::getValidatorFactory()->make($attributes, $rules);
26
        if (true === $validator->fails()) {
27
            $message = $validator->messages()->first();
28
29
            $exception = new ValidationException();
30
            $exception->statusMessage = $exception->statusMessage . ': ' . $message;
31
            $exception->validation = $validator->messages();
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator->messages() of type Illuminate\Support\MessageBag is incompatible with the declared type array of property $validation.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
33
            throw $exception;
34
        }
35
    }
36
37
    /**
38
     * @return ValidatorFactory
39
     */
40
    protected static function getValidatorFactory()
41
    {
42
        return app(ValidatorFactory::class);
43
    }
44
}
45