1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xabbuh\XApi\Client\Request; |
13
|
|
|
|
14
|
|
|
use Http\Client\Exception; |
15
|
|
|
use Http\Client\HttpClient; |
16
|
|
|
use Http\Message\RequestFactory; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
use Xabbuh\XApi\Common\Exception\AccessDeniedException; |
19
|
|
|
use Xabbuh\XApi\Common\Exception\ConflictException; |
20
|
|
|
use Xabbuh\XApi\Common\Exception\NotFoundException; |
21
|
|
|
use Xabbuh\XApi\Common\Exception\XApiException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Prepares and executes xAPI HTTP requests. |
25
|
|
|
* |
26
|
|
|
* @author Christian Flothmann <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class Handler implements HandlerInterface |
29
|
|
|
{ |
30
|
|
|
private $httpClient; |
31
|
|
|
private $requestFactory; |
32
|
|
|
private $baseUri; |
33
|
|
|
private $version; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param HttpClient $httpClient The HTTP client sending requests to the remote LRS |
37
|
|
|
* @param RequestFactory $requestFactory The factory used to create PSR-7 HTTP requests |
38
|
|
|
* @param string $baseUri The APIs base URI (all end points will be created relatively to this URI) |
39
|
|
|
* @param string $version The xAPI version |
40
|
|
|
*/ |
41
|
|
|
public function __construct(HttpClient $httpClient, RequestFactory $requestFactory, $baseUri, $version) |
42
|
|
|
{ |
43
|
|
|
$this->httpClient = $httpClient; |
44
|
|
|
$this->requestFactory = $requestFactory; |
45
|
|
|
$this->baseUri = $baseUri; |
46
|
|
|
$this->version = $version; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function createRequest($method, $uri, array $urlParameters = array(), $body = null, array $headers = array()) |
53
|
|
|
{ |
54
|
|
|
if (!in_array(strtoupper($method), array('GET', 'POST', 'PUT', 'DELETE'))) { |
55
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is no valid HTTP method (expected one of [GET, POST, PUT, DELETE]) in an xAPI context.', $method)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$uri = rtrim($this->baseUri, '/').'/'.ltrim($uri, '/'); |
59
|
|
|
|
60
|
|
|
if (count($urlParameters) > 0) { |
61
|
|
|
$uri .= '?'.http_build_query($urlParameters); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!isset($headers['X-Experience-API-Version'])) { |
65
|
|
|
$headers['X-Experience-API-Version'] = $this->version; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!isset($headers['Content-Type'])) { |
69
|
|
|
$headers['Content-Type'] = 'application/json'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->requestFactory->createRequest(strtoupper($method), $uri, $headers, $body); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritDoc} |
77
|
|
|
*/ |
78
|
|
|
public function executeRequest(RequestInterface $request, array $validStatusCodes) |
79
|
|
|
{ |
80
|
|
|
try { |
81
|
|
|
$response = $this->httpClient->sendRequest($request); |
82
|
|
|
} catch (Exception $e) { |
83
|
|
|
throw new XApiException($e->getMessage(), $e->getCode(), $e); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// catch some common errors |
87
|
|
|
if (in_array($response->getStatusCode(), array(401, 403))) { |
88
|
|
|
throw new AccessDeniedException( |
89
|
|
|
(string) $response->getBody(), |
90
|
|
|
$response->getStatusCode() |
91
|
|
|
); |
92
|
|
|
} elseif (404 === $response->getStatusCode()) { |
93
|
|
|
throw new NotFoundException((string) $response->getBody()); |
94
|
|
|
} elseif (409 === $response->getStatusCode()) { |
95
|
|
|
throw new ConflictException((string) $response->getBody()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!in_array($response->getStatusCode(), $validStatusCodes)) { |
99
|
|
|
throw new XApiException((string) $response->getBody(), $response->getStatusCode()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|