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 Guzzle\Http\ClientInterface; |
15
|
|
|
use Guzzle\Http\Exception\ClientErrorResponseException; |
16
|
|
|
use Guzzle\Http\Message\RequestInterface; |
17
|
|
|
use Xabbuh\XApi\Common\Exception\AccessDeniedException; |
18
|
|
|
use Xabbuh\XApi\Common\Exception\ConflictException; |
19
|
|
|
use Xabbuh\XApi\Common\Exception\NotFoundException; |
20
|
|
|
use Xabbuh\XApi\Common\Exception\XApiException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Prepare and execute xAPI HTTP requests. |
24
|
|
|
* |
25
|
|
|
* @author Christian Flothmann <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Handler implements HandlerInterface |
28
|
|
|
{ |
29
|
|
|
private $httpClient; |
30
|
|
|
|
31
|
|
|
private $version; |
32
|
|
|
|
33
|
|
|
private $username; |
34
|
|
|
|
35
|
|
|
private $password; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ClientInterface $httpClient The HTTP client |
39
|
|
|
* @param string $version The xAPI version |
40
|
|
|
* @param string $username The optional HTTP auth username |
41
|
|
|
* @param string $password The optional HTTP auth password |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ClientInterface $httpClient, $version, $username = null, $password = null) |
44
|
|
|
{ |
45
|
|
|
$this->httpClient = $httpClient; |
46
|
|
|
$this->version = $version; |
47
|
|
|
$this->username = $username; |
48
|
|
|
$this->password = $password; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the HTTP client used to perform the API requests. |
53
|
|
|
* |
54
|
|
|
* @return ClientInterface The HTTP client |
55
|
|
|
*/ |
56
|
|
|
public function getHttpClient() |
57
|
|
|
{ |
58
|
|
|
return $this->httpClient; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function getUsername() |
65
|
|
|
{ |
66
|
|
|
return $this->username; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function getPassword() |
73
|
|
|
{ |
74
|
|
|
return $this->password; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function createRequest($method, $uri, array $urlParameters = array(), $body = null) |
81
|
|
|
{ |
82
|
|
|
if (count($urlParameters) > 0) { |
83
|
|
|
$uri .= '?'.http_build_query($urlParameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
switch ($method) { |
87
|
|
|
case 'get': |
88
|
|
|
$request = $this->httpClient->get($uri); |
89
|
|
|
break; |
90
|
|
|
case 'post': |
91
|
|
|
$request = $this->httpClient->post($uri, null, $body); |
92
|
|
|
break; |
93
|
|
|
case 'put': |
94
|
|
|
$request = $this->httpClient->put($uri, null, $body); |
95
|
|
|
break; |
96
|
|
|
case 'delete': |
97
|
|
|
$request = $this->httpClient->delete($uri); |
98
|
|
|
break; |
99
|
|
|
default: |
100
|
|
|
throw new \InvalidArgumentException( |
101
|
|
|
$method.' is no valid HTTP method' |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$request->addHeader('X-Experience-API-Version', $this->version); |
106
|
|
|
$request->addHeader('Content-Type', 'application/json'); |
107
|
|
|
$request->setAuth($this->username, $this->password); |
108
|
|
|
|
109
|
|
|
return $request; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritDoc} |
114
|
|
|
*/ |
115
|
|
|
public function executeRequest(RequestInterface $request, array $validStatusCodes) |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$response = $request->send(); |
119
|
|
|
} catch (ClientErrorResponseException $e) { |
120
|
|
|
$response = $e->getResponse(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// catch some common errors |
124
|
|
|
if (in_array($response->getStatusCode(), array(401, 403))) { |
125
|
|
|
throw new AccessDeniedException( |
126
|
|
|
$response->getBody(true), |
127
|
|
|
$response->getStatusCode() |
128
|
|
|
); |
129
|
|
|
} elseif (404 === $response->getStatusCode()) { |
|
|
|
|
130
|
|
|
throw new NotFoundException($response->getBody(true)); |
131
|
|
|
} elseif (409 === $response->getStatusCode()) { |
|
|
|
|
132
|
|
|
throw new ConflictException($response->getBody(true)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!in_array($response->getStatusCode(), $validStatusCodes)) { |
136
|
|
|
throw new XApiException($response->getBody(true), $response->getStatusCode()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $response; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|