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) |
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
|
|
|
$request = $this->requestFactory->createRequest(strtoupper($method), $uri, array( |
65
|
|
|
'X-Experience-API-Version' => $this->version, |
66
|
|
|
'Content-Type' => 'application/json', |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
return $request; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function executeRequest(RequestInterface $request, array $validStatusCodes) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
|
|
$response = $this->httpClient->sendRequest($request); |
79
|
|
|
} catch (Exception $e) { |
80
|
|
|
throw new XApiException($e->getMessage(), $e->getCode(), $e); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// catch some common errors |
84
|
|
|
if (in_array($response->getStatusCode(), array(401, 403))) { |
85
|
|
|
throw new AccessDeniedException( |
86
|
|
|
(string) $response->getBody(), |
87
|
|
|
$response->getStatusCode() |
88
|
|
|
); |
89
|
|
|
} elseif (404 === $response->getStatusCode()) { |
90
|
|
|
throw new NotFoundException((string) $response->getBody()); |
91
|
|
|
} elseif (409 === $response->getStatusCode()) { |
92
|
|
|
throw new ConflictException((string) $response->getBody()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!in_array($response->getStatusCode(), $validStatusCodes)) { |
96
|
|
|
throw new XApiException((string) $response->getBody(), $response->getStatusCode()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|