Passed
Push — master ( 598e13...ed238e )
by Byron
47s queued 11s
created

JsonSchemaValidator::after()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Webtools\JsonSchemaRequest\Validation;
4
5
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
6
use Illuminate\Support\MessageBag;
7
use JsonSchema\Validator as SchemaValidator;
8
use JsonSchema\Constraints\Constraint;
9
use Webtools\JsonSchemaRequest\Exceptions\ValidationException;
10
11
class JsonSchemaValidator implements ValidatorContract
12
{
13
    protected SchemaValidator $validator;
14
    protected array $schema;
15
    protected array $data;
16
    protected ?MessageBag $failedConstraints = null;
17
    protected ?MessageBag $messages = null;
18
19
    /**
20
     * Array of callbacks to be executed after validation
21
     *
22
     * @var \Closure[]
23
     */
24
    private array $after = [];
25
26 8
    public function __construct(SchemaValidator $validator, array $schema, array $data)
27
    {
28 8
        $this->validator = $validator;
29 8
        $this->schema = $schema;
30 8
        $this->data = $data;
31 8
    }
32
33 8
    public function passes(): bool
34
    {
35 8
        $this->messages = new MessageBag();
36 8
        $this->failedConstraints = new MessageBag();
37
38 8
        $this->validator->validate($this->data, $this->schema, Constraint::CHECK_MODE_TYPE_CAST);
39
40 8
        foreach ($this->validator->getErrors(SchemaValidator::ERROR_DOCUMENT_VALIDATION) as $error) {
41 4
            $this->messages->add($error['property'], $error['message']);
42 4
            $this->failedConstraints->add($error['property'], $error['constraint']);
43
        }
44
45 8
        foreach ($this->after as $after) {
46 1
            $after();
47
        }
48
49 8
        return $this->messages->isEmpty();
50
    }
51
52 2
    public function getMessageBag()
53
    {
54 2
        return $this->messages;
55
    }
56
57
    /**
58
     * @inheritDoc
59
     * @throws ValidationException
60
     */
61 4
    public function validate()
62
    {
63 4
        if ($this->fails()) {
64 2
            $this->signalFailure();
65
        }
66
67 2
        return $this->validated();
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 5
    public function validated()
74
    {
75 5
        if (!$this->messages) {
76 2
            $this->passes();
77
        }
78
79 5
        if ($this->messages->isNotEmpty()) {
80 1
            $this->signalFailure();
81
        }
82
83 4
        return $this->data;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89 6
    public function fails()
90
    {
91 6
        return !$this->passes();
92
    }
93
94
    /**
95
     * Returns a list of the failed constraints for each property
96
     * @return array
97
     */
98 1
    public function failed()
99
    {
100 1
        return $this->failedConstraints->messages();
0 ignored issues
show
Bug introduced by
The method messages() does not exist on null. ( Ignorable by Annotation )

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

100
        return $this->failedConstraints->/** @scrutinizer ignore-call */ messages();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
    }
102
103
    /**
104
     * This is a NO-OP, was only added to support the ValidatorContract,
105
     * Rules cannot be applied to a schema in this manner.
106
     */
107 1
    public function sometimes($attribute, $rules, callable $callback)
108
    {
109 1
        return $this;
110
    }
111
112
    /**
113
     * @inheritDoc
114
     */
115 1
    public function after($callback)
116
    {
117
        $this->after[] = function () use ($callback) {
118 1
            return call_user_func_array($callback, [$this]);
119
        };
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 2
    public function errors()
128
    {
129 2
        return $this->getMessageBag();
130
    }
131
132 3
    private function signalFailure()
133
    {
134 3
        throw new ValidationException($this);
135
    }
136
}
137