|
1
|
|
|
<?php |
|
2
|
|
|
// This file is part of Moodle - http://moodle.org/ |
|
3
|
|
|
// |
|
4
|
|
|
// Moodle is free software: you can redistribute it and/or modify |
|
5
|
|
|
// it under the terms of the GNU General Public License as published by |
|
6
|
|
|
// the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
// (at your option) any later version. |
|
8
|
|
|
// |
|
9
|
|
|
// Moodle is distributed in the hope that it will be useful, |
|
10
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
// GNU General Public License for more details. |
|
13
|
|
|
// |
|
14
|
|
|
// You should have received a copy of the GNU General Public License |
|
15
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The assignsubmission_edulegit API client response class. |
|
19
|
|
|
* |
|
20
|
|
|
* This class encapsulates the response from an API request made using the edulegit_client. |
|
21
|
|
|
* |
|
22
|
|
|
* @package assignsubmission_edulegit |
|
23
|
|
|
* @author Alex Crosby <[email protected]> |
|
24
|
|
|
* @copyright @2024 EduLegit.com |
|
25
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace assignsubmission_edulegit; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class edulegit_client_response |
|
32
|
|
|
* |
|
33
|
|
|
* This class manages and processes the response from an API call made by the edulegit_client. |
|
34
|
|
|
*/ |
|
35
|
|
|
class edulegit_client_response { |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Indicates whether the API request was successful. |
|
39
|
|
|
* |
|
40
|
|
|
* @var bool|null |
|
41
|
|
|
*/ |
|
42
|
|
|
protected ?bool $success = null; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The HTTP response code. |
|
46
|
|
|
* |
|
47
|
|
|
* @var int|null |
|
48
|
|
|
*/ |
|
49
|
|
|
protected ?int $code = null; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The URL that was accessed during the API request. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string|null |
|
55
|
|
|
*/ |
|
56
|
|
|
protected ?string $url = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Error message, if any, from the API request. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected string $error = ''; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Information about the cURL response. |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected array $info = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* The raw body of the API response. |
|
74
|
|
|
* |
|
75
|
|
|
* @var string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected string $body = ''; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Decoded response payload. |
|
81
|
|
|
* |
|
82
|
|
|
* @var mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
protected mixed $payload = null; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Edulegit_client_response constructor. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $body The raw body of the API response. |
|
90
|
|
|
* @param array $info Array of cURL response information. |
|
91
|
|
|
* @param string $error The error message, if any. |
|
92
|
|
|
* @param string|null $url The URL used in the API request. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function __construct(string $body, array $info, string $error, ?string $url = null) { |
|
95
|
|
|
$this->body = $body; |
|
96
|
|
|
$this->info = $info; |
|
97
|
|
|
$this->error = $error; |
|
98
|
|
|
$this->url = $url; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Retrieves the success status of the API request. |
|
103
|
|
|
* |
|
104
|
|
|
* Determines success based on the HTTP response code and presence of errors. |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool True if the request was successful, false otherwise. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function get_success(): bool { |
|
109
|
|
|
if ($this->success === null) { |
|
110
|
|
|
$this->success = ($this->get_code() >= 200 && $this->get_code() < 300 && !$this->error); |
|
111
|
|
|
} |
|
112
|
|
|
return $this->success; |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets the success status of the API request. |
|
117
|
|
|
* |
|
118
|
|
|
* @param bool $success The success status to set. |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function set_success($success): void { |
|
122
|
|
|
$this->success = (bool) $success; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Retrieves the HTTP response code from the API request. |
|
127
|
|
|
* |
|
128
|
|
|
* @return int The HTTP response code. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function get_code(): int { |
|
131
|
|
|
if ($this->code === null) { |
|
132
|
|
|
$this->code = (int) $this->info['http_code'] ?? 0; |
|
133
|
|
|
} |
|
134
|
|
|
return $this->code; |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sets the HTTP response code for the API request. |
|
139
|
|
|
* |
|
140
|
|
|
* @param int|null $code The HTTP response code to set. |
|
141
|
|
|
* @return void |
|
142
|
|
|
*/ |
|
143
|
|
|
public function set_code(?int $code): void { |
|
144
|
|
|
$this->code = $code; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Retrieves the URL used in the API request. |
|
149
|
|
|
* |
|
150
|
|
|
* @return string|null The URL of the API request. |
|
151
|
|
|
*/ |
|
152
|
|
|
public function get_url(): ?string { |
|
153
|
|
|
return $this->url; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets the URL used in the API request. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string|null $url The URL to set. |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
public function set_url(?string $url): void { |
|
163
|
|
|
$this->url = $url; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Retrieves the error message from the API request. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string|null The error message, or null if none exists. |
|
170
|
|
|
*/ |
|
171
|
|
|
public function get_error(): ?string { |
|
172
|
|
|
return $this->error; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Sets the error message for the API request. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string|null $error The error message to set. |
|
179
|
|
|
* @return void |
|
180
|
|
|
*/ |
|
181
|
|
|
public function set_error(?string $error): void { |
|
182
|
|
|
$this->error = $error; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Retrieves the cURL response information. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array An array of cURL response information. |
|
189
|
|
|
*/ |
|
190
|
|
|
public function get_info(): array { |
|
191
|
|
|
return $this->info ?? []; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Sets the cURL response information. |
|
196
|
|
|
* |
|
197
|
|
|
* @param array $info The cURL response information to set. |
|
198
|
|
|
* @return void |
|
199
|
|
|
*/ |
|
200
|
|
|
public function set_info(array $info): void { |
|
201
|
|
|
$this->info = $info; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Retrieves the raw body of the API response. |
|
206
|
|
|
* |
|
207
|
|
|
* @return string|null The raw body of the response. |
|
208
|
|
|
*/ |
|
209
|
|
|
public function get_body(): ?string { |
|
210
|
|
|
return $this->body; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Sets the raw body of the API response. |
|
215
|
|
|
* |
|
216
|
|
|
* @param string|null $body The raw body to set. |
|
217
|
|
|
* @return void |
|
218
|
|
|
*/ |
|
219
|
|
|
public function set_body(?string $body): void { |
|
220
|
|
|
$this->body = $body; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Retrieves the decoded payload from the API response body. |
|
225
|
|
|
* |
|
226
|
|
|
* @return mixed The decoded payload. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function get_payload(): mixed { |
|
229
|
|
|
if ($this->payload === null) { |
|
230
|
|
|
$this->payload = edulegit_helper::json_decode($this->get_body()); |
|
231
|
|
|
} |
|
232
|
|
|
return $this->payload; |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|