Passed
Push — master ( d7d8c0...c26592 )
by Mads
03:35
created

ValidateTrait::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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
trait ValidateTrait
10
{
11
    /**
12
     * @param array $attributes
13
     * @param array $rules
14
     * @return void
15
     * @throws ValidationException
16
     */
17
    public static function validate(array $attributes, array $rules)
18
    {
19
        /** @var Validator $validator */
20
        $validator = static::getValidatorFactory()->make($attributes, $rules);
21
        if (true === $validator->fails()) {
22
            $message = $validator->messages()->first();
23
24
            $exception = new ValidationException;
25
            $exception->statusMessage = $exception->statusMessage . ': ' . $message;
26
27
            throw $exception;
28
        }
29
    }
30
31
    /**
32
     * @return ValidatorFactory
33
     */
34
    protected static function getValidatorFactory()
35
    {
36
        return app(ValidatorFactory::class);
37
    }
38
}
39