Completed
Push — master ( 41116d...43e4d4 )
by
unknown
13:57
created

Request::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace RealPage\JsonApi\Requests;
4
5
use Illuminate\Contracts\Validation\Validator;
6
use Illuminate\Http\Request as IlluminateRequest;
7
use Neomerx\JsonApi\Exceptions\JsonApiException;
8
use RealPage\JsonApi\Validation\RequestFailedValidation;
9
10
class Request
11
{
12
    /** @var array */
13
    protected $json;
14
15
    /** @var IlluminateRequest */
16
    protected $request;
17
18
    /** @var Validator */
19
    protected $validator;
20
21
    public function __construct(IlluminateRequest $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
22
    {
23
        $this->request = $request;
24
    }
25
26
    /**
27
     * @throws RequestFailedValidation
28
     */
29
    public function validate()
30
    {
31
        $validator = $this->validator();
32
33
        if ($validator->isValid($this) === false) {
0 ignored issues
show
Bug introduced by
The method isValid() does not seem to exist on object<Illuminate\Contracts\Validation\Validator>.

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...
34
            throw new RequestFailedValidation($validator->errors());
0 ignored issues
show
Bug introduced by
The method errors() does not seem to exist on object<Illuminate\Contracts\Validation\Validator>.

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...
35
        }
36
    }
37
38
    public function json(): array
39
    {
40
        if (!isset($this->json)) {
41
            $json = json_decode(
42
                $this->request()->getContent(),
43
                true, // associative array
44
                512, // depth
45
                JSON_UNESCAPED_SLASHES
46
            );
47
48
            if (JSON_ERROR_NONE !== json_last_error()) {
49
                throw new JsonApiException(new MalformedRequest());
50
            }
51
52
            $this->json = $json;
53
        }
54
55
        return $this->json;
56
    }
57
58
    public function validator() : Validator
59
    {
60
        return $this->validator;
61
    }
62
63
    public function setValidator(Validator $validator)
64
    {
65
        $this->validator = $validator;
66
    }
67
68
    public function request(): IlluminateRequest
69
    {
70
        return $this->request;
71
    }
72
}
73