LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Service.php
1<?php
2
3namespace ceLTIc\LTI\Service;
4
10
19{
20
26 public $unsigned = false;
27
33 protected $endpoint = null;
34
40 protected $scope = null;
41
47 protected $mediaType = null;
48
54 private $platform = null;
55
61 private $http = null;
62
69 public function __construct($platform, $endpoint)
70 {
71 $this->platform = $platform;
72 $this->endpoint = $endpoint;
73 }
74
83 public function getConsumer()
84 {
85 Util::logDebug('Method ceLTIc\LTI\Service::getConsumer() has been deprecated; please use ceLTIc\LTI\Service::getPlatform() instead.',
86 true);
87 return $this->getPlatform();
88 }
89
95 public function getPlatform()
96 {
97 return $this->platform;
98 }
99
105 public function getScope()
106 {
107 return $this->scope;
108 }
109
119 public function send($method, $parameters = array(), $body = null)
120 {
121 $url = $this->endpoint;
122 if (!empty($parameters)) {
123 if (strpos($url, '?') === false) {
124 $sep = '?';
125 } else {
126 $sep = '&';
127 }
128 foreach ($parameters as $name => $value) {
129 $url .= $sep . urlencode($name) . '=' . urlencode($value);
130 $sep = '&';
131 }
132 }
133 $header = null;
134 $retry = !$this->platform->useOAuth1();
135 $newToken = false;
136 $retried = false;
137 do {
138 if (!$this->unsigned) {
139 $accessToken = $this->platform->getAccessToken();
140 if (!$this->platform->useOAuth1()) {
141 if (empty($accessToken)) {
142 $accessToken = new AccessToken($this->platform);
143 $this->platform->setAccessToken($accessToken);
144 }
145 if (!$accessToken->hasScope($this->scope)) {
146 $accessToken->get($this->scope);
147 $newToken = true;
148 if (!$accessToken->hasScope($this->scope)) { // Try obtaining a token for just this scope
149 $accessToken->expires = time();
150 $accessToken->get($this->scope, true);
151 $retried = true;
152 if (!$accessToken->hasScope($this->scope)) {
153 if (empty($this->http)) {
154 $this->http = new HttpMessage($url);
155 $this->http->error = "Unable to obtain an access token for scope: {$this->scope}";
156 }
157 break;
158 }
159 }
160 }
161 }
162 $header = $this->platform->signServiceRequest($url, $method, $this->mediaType, $body);
163 }
164// Connect to platform and parse JSON response
165 $this->http = new HttpMessage($url, $method, $body, $header);
166 if ($this->http->send() && !empty($this->http->response)) {
167 $this->http->responseJson = Util::jsonDecode($this->http->response);
168 $this->http->ok = !is_null($this->http->responseJson);
169 }
170 $retry = $retry && !$retried && !$this->http->ok;
171 if ($retry) {
172 if (!$newToken) { // Invalidate existing token to force a new one to be obtained
173 $accessToken->expires = time();
174 $newToken = true;
175 } elseif (count($accessToken->scopes) !== 1) { // Try obtaining a token for just this scope
176 $accessToken->expires = time();
177 $accessToken->get($this->scope, true);
178 $retried = true;
179 } else {
180 $retry = false;
181 }
182 }
183 } while ($retry);
184
185 return $this->http;
186 }
187
193 public function getHttpMessage()
194 {
195 return $this->http;
196 }
197
198###
199### PROTECTED METHODS
200###
201
210 protected function parseContextsInArray($contexts, $arr)
211 {
212 if (is_array($contexts)) {
213 $contextdefs = array();
214 foreach ($contexts as $context) {
215 if (is_object($context)) {
216 $contextdefs = array_merge(get_object_vars($context), $contexts);
217 }
218 }
219 $parsed = array();
220 foreach ($arr as $key => $value) {
221 $parts = explode(':', $value, 2);
222 if (count($parts) > 1) {
223 if (array_key_exists($parts[0], $contextdefs)) {
224 $parsed[$key] = $contextdefs[$parts[0]] . $parts[1];
225 break;
226 }
227 }
228 $parsed[$key] = $value;
229 }
230 } else {
231 $parsed = $arr;
232 }
233
234 return $parsed;
235 }
236
237}
Class to represent an HTTP message.
Class to represent an HTTP message request.
Class to represent a platform.
Definition Platform.php:18
Class to implement a service.
Definition Service.php:19
getScope()
Get access scope.
Definition Service.php:105
__construct($platform, $endpoint)
Class constructor.
Definition Service.php:69
$scope
Service access scope.
Definition Service.php:40
getConsumer()
Get tool consumer.
Definition Service.php:83
getHttpMessage()
Get HttpMessage object for last request.
Definition Service.php:193
send($method, $parameters=array(), $body=null)
Send a service request.
Definition Service.php:119
getPlatform()
Get platform.
Definition Service.php:95
$mediaType
Media type of message body.
Definition Service.php:47
$unsigned
Whether service request should be sent unsigned.
Definition Service.php:26
$endpoint
Service endpoint.
Definition Service.php:33
parseContextsInArray($contexts, $arr)
Parse the JSON for context references.
Definition Service.php:210
Class to represent a tool consumer.
Class to implement utility methods.
Definition Util.php:15
static jsonDecode($str, $associative=false)
Decode a JSON string.
Definition Util.php:560
static logDebug($message, $showSource=false)
Log a debug message.
Definition Util.php:274