ServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 20
c 1
b 0
f 0
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A boot() 0 28 3
1
<?php declare(strict_types = 1);
2
3
namespace RamosHenrique\JsonSchemaValidator;
4
5
use Exception;
6
7
use Illuminate\Support\Facades\Validator as BaseValidator;
8
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
9
use Illuminate\Validation\ValidationException;
10
11
class ServiceProvider extends BaseServiceProvider
12
{
13
    public function boot()
14
    {
15
        $this->publishes(
16
            [
17
            __DIR__.'/../config/json_schema_validator.php' => config_path('json_schema_validator.php'),
18
            ],
19
            'json_schema_validator'
20
        );
21
22
        BaseValidator::extend('json_schema_validator', static function ($attribute, $value, $parameters, $validator) {
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
        BaseValidator::extend('json_schema_validator', static function ($attribute, $value, $parameters, /** @scrutinizer ignore-unused */ $validator) {

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

Loading history...
23
            if (count($parameters) != 1) {
24
                throw ValidationException::withMessages([
25
                    'json_schema_validator' => JsonSchemaValidatorException::INVALID_PARAMETERS_QUANTITY,
26
                ]);
27
            }
28
29
            $filePath = config('json_schema_validator.storage_path') . '/' . $parameters[0];
30
31
            try {
32
                $JsonValidate = new JsonSchemaValidator();
33
34
                $JsonValidate->setSchema($filePath);
35
                $JsonValidate->setData($value);
36
37
                return $JsonValidate->validate();
38
            } catch (Exception $e) {
39
                throw ValidationException::withMessages([
40
                    'json_schema_validator' => $e->getMessage(),
41
                ]);
42
            }
43
        });
44
    }
45
46
    /**
47
    * Make config publishment optional by merging the config from the package.
48
    *
49
    * @return  void
50
    */
51
    public function register()
52
    {
53
        $this->mergeConfigFrom(
54
            __DIR__.'/../config/json_schema_validator.php',
55
            'json_schema_validator'
56
        );
57
    }
58
}
59