JsonApiValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 8.96 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validateAsJsonApi() 6 47 4

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
3
namespace Nestecha\LaravelJsonApiValidation;
4
5
use CloudCreativity\LaravelJsonApi\Factories\Factory;
6
use CloudCreativity\LaravelJsonApi\LaravelJsonApi;
7
use Illuminate\Validation\ValidationException;
8
use Neomerx\JsonApi\Document\Error;
9
use Nestecha\LaravelJsonApiValidation\Exception\JsonApiValidationException;
10
11
class JsonApiValidator
12
{
13
    /**
14
     * @var string
15
     */
16
    private $configName;
17
18
    public function __construct(string $configName = 'json-api-validation')
19
    {
20
        $this->configName = $configName;
21
    }
22
23
    /**
24
     * @param  array  $data
25
     * @param  array  $rules
26
     * @param  array  $messages
27
     * @param  array  $customAttributes
28
     * @throws JsonApiValidationException
29
     */
30
    public function validateAsJsonApi(array $data, array $rules, array $messages = [], array $customAttributes = [])
31
    {
32
        $originalValidationFailuresSetting = LaravelJsonApi::$validationFailures;
33
        LaravelJsonApi::showValidatorFailures();
34
35
        /** @var Factory $factory */
36
        $factory = app(Factory::class);
37
        $configName = $this->configName;
38
39
        $validator = $factory->createValidator(
40
            $data,
41
            $rules,
42
            $messages,
43
            $customAttributes,
44
            function ($field, $message, $failures) use ($configName, $data)
45
            {
46
                $errorCode = null;
47 View Code Duplication
                if (!empty(config("{$configName}.{$failures['rule']}.code"))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
                    $errorCode = config("{$configName}.{$failures['rule']}.code");
49
                }
50
51
                $fieldType = (new GetFieldType())->getType($field);
52 View Code Duplication
                if (!empty(config("{$configName}.{$failures['rule']}.$fieldType.code"))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
                    $errorCode = config("{$configName}.{$failures['rule']}.$fieldType.code");
54
                }
55
56
                return new Error(
57
                    null,
58
                    null,
59
                    422,
60
                    $errorCode,
61
                    "Unprocessable Entity",
62
                    $message,
63
                    ['pointer' => "/data/attributes/$field", 'value' => data_get($data, $field)],
64
                    ['failed' => $failures]
65
                );
66
            }
67
        );
68
69
        try {
70
            $validator->validate();
71
        } catch (ValidationException $validationException) {
72
            throw new JsonApiValidationException($validator);
73
        } finally {
74
            LaravelJsonApi::$validationFailures = $originalValidationFailuresSetting;
75
        }
76
    }
77
}