| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Packback\Lti1p3;
|
|
|
4 |
|
|
|
5 |
use Packback\Lti1p3\Interfaces\IServiceRequest;
|
|
|
6 |
|
|
|
7 |
class ServiceRequest implements IServiceRequest
|
|
|
8 |
{
|
|
|
9 |
// Request methods
|
|
|
10 |
public const METHOD_DELETE = 'DELETE';
|
|
|
11 |
public const METHOD_GET = 'GET';
|
|
|
12 |
public const METHOD_POST = 'POST';
|
|
|
13 |
public const METHOD_PUT = 'PUT';
|
|
|
14 |
|
|
|
15 |
// Request types
|
|
|
16 |
public const TYPE_UNSUPPORTED = 'unsupported';
|
|
|
17 |
public const TYPE_AUTH = 'auth';
|
|
|
18 |
|
|
|
19 |
// MessageLaunch
|
|
|
20 |
public const TYPE_GET_KEYSET = 'get_keyset';
|
|
|
21 |
|
|
|
22 |
// AGS
|
|
|
23 |
public const TYPE_GET_GRADES = 'get_grades';
|
|
|
24 |
public const TYPE_SYNC_GRADE = 'sync_grades';
|
|
|
25 |
public const TYPE_CREATE_LINEITEM = 'create_lineitem';
|
|
|
26 |
public const TYPE_DELETE_LINEITEM = 'delete_lineitem';
|
|
|
27 |
public const TYPE_GET_LINEITEMS = 'get_lineitems';
|
|
|
28 |
public const TYPE_GET_LINEITEM = 'get_lineitem';
|
|
|
29 |
public const TYPE_UPDATE_LINEITEM = 'update_lineitem';
|
|
|
30 |
|
|
|
31 |
// CGS
|
|
|
32 |
public const TYPE_GET_GROUPS = 'get_groups';
|
|
|
33 |
public const TYPE_GET_SETS = 'get_sets';
|
|
|
34 |
|
|
|
35 |
// NRPS
|
|
|
36 |
public const TYPE_GET_MEMBERSHIPS = 'get_memberships';
|
|
|
37 |
private $body;
|
|
|
38 |
private $payload;
|
|
|
39 |
private $accessToken;
|
|
|
40 |
private $contentType = 'application/json';
|
|
|
41 |
private $accept = 'application/json';
|
|
|
42 |
|
|
|
43 |
// Other
|
|
|
44 |
private $maskResponseLogs = false;
|
|
|
45 |
|
|
|
46 |
public function __construct(
|
|
|
47 |
private string $method,
|
|
|
48 |
private string $url,
|
|
|
49 |
private string $type = self::TYPE_UNSUPPORTED
|
|
|
50 |
) {
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function getMethod(): string
|
|
|
54 |
{
|
|
|
55 |
return strtoupper($this->method);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function getUrl(): string
|
|
|
59 |
{
|
|
|
60 |
return $this->url;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function getPayload(): array
|
|
|
64 |
{
|
|
|
65 |
if (isset($this->payload)) {
|
|
|
66 |
return $this->payload;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$payload = [
|
|
|
70 |
'headers' => $this->getHeaders(),
|
|
|
71 |
];
|
|
|
72 |
|
|
|
73 |
$body = $this->getBody();
|
|
|
74 |
if ($body) {
|
|
|
75 |
$payload['body'] = $body;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return $payload;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function setUrl(string $url): IServiceRequest
|
|
|
82 |
{
|
|
|
83 |
$this->url = $url;
|
|
|
84 |
|
|
|
85 |
return $this;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public function setAccessToken(string $accessToken): IServiceRequest
|
|
|
89 |
{
|
|
|
90 |
$this->accessToken = 'Bearer '.$accessToken;
|
|
|
91 |
|
|
|
92 |
return $this;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function setBody(string $body): IServiceRequest
|
|
|
96 |
{
|
|
|
97 |
$this->body = $body;
|
|
|
98 |
|
|
|
99 |
return $this;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
public function setPayload(array $payload): IServiceRequest
|
|
|
103 |
{
|
|
|
104 |
$this->payload = $payload;
|
|
|
105 |
|
|
|
106 |
return $this;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public function setAccept(string $accept): IServiceRequest
|
|
|
110 |
{
|
|
|
111 |
$this->accept = $accept;
|
|
|
112 |
|
|
|
113 |
return $this;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public function setContentType(string $contentType): IServiceRequest
|
|
|
117 |
{
|
|
|
118 |
$this->contentType = $contentType;
|
|
|
119 |
|
|
|
120 |
return $this;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public function getMaskResponseLogs(): bool
|
|
|
124 |
{
|
|
|
125 |
return $this->maskResponseLogs;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
public function setMaskResponseLogs(bool $shouldMask): IServiceRequest
|
|
|
129 |
{
|
|
|
130 |
$this->maskResponseLogs = $shouldMask;
|
|
|
131 |
|
|
|
132 |
return $this;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public function getErrorPrefix(): string
|
|
|
136 |
{
|
|
|
137 |
$defaultMessage = 'Logging request data:';
|
|
|
138 |
$errorMessages = [
|
|
|
139 |
static::TYPE_UNSUPPORTED => $defaultMessage,
|
|
|
140 |
static::TYPE_AUTH => 'Authenticating:',
|
|
|
141 |
static::TYPE_GET_KEYSET => 'Getting key set:',
|
|
|
142 |
static::TYPE_GET_GRADES => 'Getting grades:',
|
|
|
143 |
static::TYPE_SYNC_GRADE => 'Syncing grade for this lti_user_id:',
|
|
|
144 |
static::TYPE_CREATE_LINEITEM => 'Creating lineitem:',
|
|
|
145 |
static::TYPE_DELETE_LINEITEM => 'Deleting lineitem:',
|
|
|
146 |
static::TYPE_GET_LINEITEMS => 'Getting lineitems:',
|
|
|
147 |
static::TYPE_GET_LINEITEM => 'Getting a lineitem:',
|
|
|
148 |
static::TYPE_UPDATE_LINEITEM => 'Updating lineitem:',
|
|
|
149 |
static::TYPE_GET_GROUPS => 'Getting groups:',
|
|
|
150 |
static::TYPE_GET_SETS => 'Getting sets:',
|
|
|
151 |
static::TYPE_GET_MEMBERSHIPS => 'Getting memberships:',
|
|
|
152 |
];
|
|
|
153 |
|
|
|
154 |
return $errorMessages[$this->type] ?? $defaultMessage;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
private function getHeaders(): array
|
|
|
158 |
{
|
|
|
159 |
$headers = [
|
|
|
160 |
'Accept' => $this->accept,
|
|
|
161 |
];
|
|
|
162 |
|
|
|
163 |
if (isset($this->accessToken)) {
|
|
|
164 |
$headers['Authorization'] = $this->accessToken;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
// Include Content-Type for POST and PUT requests
|
|
|
168 |
if (in_array($this->getMethod(), [ServiceRequest::METHOD_POST, ServiceRequest::METHOD_PUT])) {
|
|
|
169 |
$headers['Content-Type'] = $this->contentType;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return $headers;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
private function getBody(): ?string
|
|
|
176 |
{
|
|
|
177 |
return $this->body;
|
|
|
178 |
}
|
|
|
179 |
}
|