AbstractRequest::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Slides\Connector\Auth\Clients;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class AbstractRequest
9
 *
10
 * @package Slides\Connector\Auth\Clients
11
 */
12
abstract class AbstractRequest
13
{
14
    /**
15
     * HTTP method
16
     *
17
     * @var string
18
     */
19
    protected $method;
20
21
    /**
22
     * Request URI
23
     *
24
     * @var string
25
     */
26
    protected $uri;
27
28
    /**
29
     * Response of the request
30
     *
31
     * @var mixed
32
     */
33
    protected $response;
34
35
    /**
36
     * @var AbstractClient
37
     */
38
    protected $client;
39
40
    /**
41
     * Request options
42
     *
43
     * @var array
44
     */
45
    protected $options = [];
46
47
    /**
48
     * Request attributes
49
     *
50
     * @var array
51
     */
52
    protected $attributes = [];
53
54
    /**
55
     * Compose a request
56
     *
57
     * @return void
58
     */
59
    abstract public function compose();
60
61
    /**
62
     * Validate a response
63
     *
64
     * @return bool
65
     */
66
    abstract public function success(): bool;
67
68
    /**
69
     * AbstractRequest constructor.
70
     *
71
     * @param AbstractClient $client
72
     * @param array $attributes
73
     * @param array $options
74
     */
75
    public function __construct(AbstractClient $client, array $attributes = [], array $options = [])
76
    {
77
        $this->client = $client;
78
        $this->setAttributes($attributes);
79
        $this->options = $options;
80
    }
81
82
    /**
83
     * Get request method
84
     *
85
     * @return string
86
     */
87
    public function getMethod(): string
88
    {
89
        return $this->method;
90
    }
91
92
    /**
93
     * Get request URI
94
     *
95
     * @return string
96
     */
97
    public function getUri(): string
98
    {
99
        return $this->uri;
100
    }
101
102
    /**
103
     * Get request options
104
     *
105
     * @return array
106
     */
107
    public function getOptions(): array
108
    {
109
        return $this->options;
110
    }
111
112
    /**
113
     * Set a response
114
     *
115
     * @param mixed $response
116
     *
117
     * @return $this
118
     */
119
    public function setResponse($response)
120
    {
121
        $this->response = $response;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Set attributes
128
     *
129
     * @param array $attributes
130
     */
131
    public function setAttributes(array $attributes)
132
    {
133
        foreach ($attributes as $name => $value) {
134
            $this->setAttribute($name, $value);
135
        }
136
    }
137
138
    /**
139
     * Retrieve an attribute value
140
     *
141
     * @param string $key
142
     * @param mixed $default
143
     *
144
     * @return mixed
145
     */
146
    public function attribute(string $key, $default = null)
147
    {
148
        return Arr::get($this->attributes, $key, $default);
149
    }
150
151
    /**
152
     * Set an attribute value
153
     *
154
     * @param string $key
155
     * @param $value
156
     */
157
    public function setAttribute(string $key, $value)
158
    {
159
        $this->attributes[$key] = $value;
160
    }
161
162
    /**
163
     * Set body params (as JSON)
164
     *
165
     * @param array $params
166
     *
167
     * @return $this
168
     */
169
    public function body(array $params)
170
    {
171
        $this->mergeOptions(['json' => $params]);
172
173
        return $this;
174
    }
175
176
    /**
177
     * Set query params
178
     *
179
     * @param array $params
180
     *
181
     * @return $this
182
     */
183
    public function query(array $params)
184
    {
185
        $this->mergeOptions(['query' => $params]);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Merge request options.
192
     *
193
     * @param array $options
194
     *
195
     * @return $this
196
     */
197
    public function mergeOptions(array $options)
198
    {
199
        $this->options = array_merge_recursive($this->options, $options);
200
201
        return $this;
202
    }
203
204
    /**
205
     * Get attribute value
206
     *
207
     * @param $name
208
     *
209
     * @return mixed
210
     */
211
    public function __get(string $name)
212
    {
213
        return $this->attribute($name);
214
    }
215
}