Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
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 $method; 38 private $url; 39 private $type; 40 private $body; 41 private $payload; 42 private $accessToken; 43 private $contentType = 'application/json'; 44 private $accept = 'application/json'; 45 46 public function __construct(string $method, string $url, $type = self::UNSUPPORTED) 47 { 48 $this->method = $method; 49 $this->url = $url; 50 $this->type = $type; 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 getErrorPrefix(): string 124 { 125 $defaultMessage = 'Logging request data:'; 126 $errorMessages = [ 127 static::TYPE_UNSUPPORTED => $defaultMessage, 128 static::TYPE_AUTH => 'Authenticating:', 129 static::TYPE_GET_KEYSET => 'Getting key set:', 130 static::TYPE_GET_GRADES => 'Getting grades:', 131 static::TYPE_SYNC_GRADE => 'Syncing grade for this lti_user_id:', 132 static::TYPE_CREATE_LINEITEM => 'Creating lineitem:', 133 static::TYPE_DELETE_LINEITEM => 'Deleting lineitem:', 134 static::TYPE_GET_LINEITEMS => 'Getting lineitems:', 135 static::TYPE_GET_LINEITEM => 'Getting a lineitem:', 136 static::TYPE_UPDATE_LINEITEM => 'Updating lineitem:', 137 static::TYPE_GET_GROUPS => 'Getting groups:', 138 static::TYPE_GET_SETS => 'Getting sets:', 139 static::TYPE_GET_MEMBERSHIPS => 'Getting memberships:', 140 ]; 141 142 return $errorMessages[$this->type] ?? $defaultMessage; 143 } 144 145 private function getHeaders(): array 146 { 147 $headers = [ 148 'Accept' => $this->accept, 149 ]; 150 151 if (isset($this->accessToken)) { 152 $headers['Authorization'] = $this->accessToken; 153 } 154 155 // Include Content-Type for POST and PUT requests 156 if (in_array($this->getMethod(), [ServiceRequest::METHOD_POST, ServiceRequest::METHOD_PUT])) { 157 $headers['Content-Type'] = $this->contentType; 158 } 159 160 return $headers; 161 } 162 163 private function getBody(): ?string 164 { 165 return $this->body; 166 } 167 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body