Completed
Pull Request — master (#6)
by
unknown
03:48 queued 10s
created

AbstractRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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