| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace IMSGlobal\LTI\ToolProvider\Service;
|
|
|
4 |
|
|
|
5 |
use IMSGlobal\LTI\ToolProvider;
|
|
|
6 |
use IMSGlobal\LTI\HTTPMessage;
|
|
|
7 |
|
|
|
8 |
/**
|
|
|
9 |
* Class to implement a service
|
|
|
10 |
*
|
|
|
11 |
* @author Stephen P Vickers <svickers@imsglobal.org>
|
|
|
12 |
* @copyright IMS Global Learning Consortium Inc
|
|
|
13 |
* @date 2016
|
|
|
14 |
* @version 3.0.0
|
|
|
15 |
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
|
|
|
16 |
*/
|
|
|
17 |
#[\AllowDynamicProperties]
|
|
|
18 |
class Service
|
|
|
19 |
{
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Whether service request should be sent unsigned.
|
|
|
23 |
*
|
|
|
24 |
* @var boolean $unsigned
|
|
|
25 |
*/
|
|
|
26 |
public $unsigned = false;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Service endpoint.
|
|
|
30 |
*
|
|
|
31 |
* @var string $endpoint
|
|
|
32 |
*/
|
|
|
33 |
protected $endpoint;
|
|
|
34 |
/**
|
|
|
35 |
* Tool Consumer for this service request.
|
|
|
36 |
*
|
|
|
37 |
* @var ToolConsumer $consumer
|
|
|
38 |
*/
|
|
|
39 |
private $consumer;
|
|
|
40 |
/**
|
|
|
41 |
* Media type of message body.
|
|
|
42 |
*
|
|
|
43 |
* @var string $mediaType
|
|
|
44 |
*/
|
|
|
45 |
private $mediaType;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Class constructor.
|
|
|
49 |
*
|
|
|
50 |
* @param ToolConsumer $consumer Tool consumer object for this service request
|
|
|
51 |
* @param string $endpoint Service endpoint
|
|
|
52 |
* @param string $mediaType Media type of message body
|
|
|
53 |
*/
|
|
|
54 |
public function __construct($consumer, $endpoint, $mediaType)
|
|
|
55 |
{
|
|
|
56 |
|
|
|
57 |
$this->consumer = $consumer;
|
|
|
58 |
$this->endpoint = $endpoint;
|
|
|
59 |
$this->mediaType = $mediaType;
|
|
|
60 |
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Send a service request.
|
|
|
65 |
*
|
|
|
66 |
* @param string $method The action type constant (optional, default is GET)
|
|
|
67 |
* @param array $parameters Query parameters to add to endpoint (optional, default is none)
|
|
|
68 |
* @param string $body Body of request (optional, default is null)
|
|
|
69 |
*
|
|
|
70 |
* @return HTTPMessage HTTP object containing request and response details
|
|
|
71 |
*/
|
|
|
72 |
public function send($method, $parameters = array(), $body = null)
|
|
|
73 |
{
|
|
|
74 |
|
|
|
75 |
$url = $this->endpoint;
|
|
|
76 |
if (!empty($parameters)) {
|
|
|
77 |
if (strpos($url, '?') === false) {
|
|
|
78 |
$sep = '?';
|
|
|
79 |
} else {
|
|
|
80 |
$sep = '&';
|
|
|
81 |
}
|
|
|
82 |
foreach ($parameters as $name => $value) {
|
|
|
83 |
$url .= $sep . urlencode($name) . '=' . urlencode($value);
|
|
|
84 |
$sep = '&';
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
if (!$this->unsigned) {
|
|
|
88 |
$header = ToolProvider\ToolConsumer::addSignature($url, $this->consumer->getKey(), $this->consumer->secret, $body, $method, $this->mediaType);
|
|
|
89 |
} else {
|
|
|
90 |
$header = null;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Connect to tool consumer
|
|
|
94 |
$http = new HTTPMessage($url, $method, $body, $header);
|
|
|
95 |
// Parse JSON response
|
|
|
96 |
if ($http->send() && !empty($http->response)) {
|
|
|
97 |
$http->responseJson = json_decode($http->response);
|
|
|
98 |
$http->ok = !is_null($http->responseJson);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $http;
|
|
|
102 |
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
}
|