Completed
Pull Request — master (#8)
by
unknown
14:35
created

Request::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\Authorization\RequestFailedAuthorization;
9
use RealPage\JsonApi\ErrorFactory;
10
use RealPage\JsonApi\Validation\RequestFailedValidation;
11
use RealPage\JsonApi\Validation\ValidatesRequests;
12
13
class Request
14
{
15
    /** @var array */
16
    protected $json;
17
18
    /** @var IlluminateRequest */
19
    protected $request;
20
21
    /** @var Validator */
22
    protected $validator;
23
24
    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...
25
    {
26
        $this->request = $request;
27
    }
28
29
    /**
30
     * @throws RequestFailedValidation
31
     */
32
    public function validate()
33
    {
34
        /** @var ValidatesRequests $validator */
35
        $validator = $this->validator();
36
37
        if ($validator->isValid($this) === false) {
38
            throw new RequestFailedValidation($validator->errors());
39
        }
40
    }
41
42
    /**
43
     * Ensure that a requested operation is authorized.
44
     * If not, throw an exception.
45
     *
46
     * This requires a registered Policy.
47
     * If no policy is defined,
48
     * the framework will throw InvalidArgumentException.
49
     *
50
     * See also:
51
     *   https://laravel.com/docs/master/authorization
52
     *   http://jsonapi.org/format/#errors
53
     *
54
     * @param string $action Desired action; must match a policy method name.
55
     * @param mixed  $object Target object; class must match a policy.
56
     * @param array  $source Reference to source of error in request.
57
     *
58
     * @return bool  True on success; throws exception on failure.
59
     *
60
     * @throws RequestFailedAuthorization
61
     *
62
     * TODO: use a UUID for the source?
63
     */
64
    public function authorize(
65
        string $action,
66
        $object,
67
        array $source = null
68
    ) {
69
        if ($this->request()->user()->cant($action, $object)) {
70
            throw new RequestFailedAuthorization(
71
                ErrorFactory::buildUnauthorized(
72
                    $id = null,
73
                    $aboutLink = null,
74
                    $code = null,
75
                    $source,
76
                    $meta = null
77
                )
78
            );
79
        }
80
81
        return true;
82
    }
83
84
    public function json(): array
85
    {
86
        if (!isset($this->json)) {
87
            $json = json_decode(
88
                $this->request()->getContent(),
89
                true, // associative array
90
                512, // depth
91
                JSON_UNESCAPED_SLASHES
92
            );
93
94
            if (JSON_ERROR_NONE !== json_last_error()) {
95
                throw new JsonApiException(new MalformedRequest());
96
            }
97
98
            $this->json = $json;
99
        }
100
101
        return $this->json;
102
    }
103
104
    public function validator() : ValidatesRequests
105
    {
106
        return $this->validator;
107
    }
108
109
    public function setValidator(ValidatesRequests $validator)
110
    {
111
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<RealPage\JsonApi\...tion\ValidatesRequests> is incompatible with the declared type object<Illuminate\Contracts\Validation\Validator> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
112
    }
113
114
    public function request(): IlluminateRequest
115
    {
116
        return $this->request;
117
    }
118
}
119