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\Api; |
13
|
|
|
|
14
|
|
|
use Xabbuh\XApi\Client\Request\HandlerInterface; |
15
|
|
|
use Xabbuh\XApi\Model\Document; |
16
|
|
|
use Xabbuh\XApi\Model\DocumentData; |
17
|
|
|
use Xabbuh\XApi\Serializer\DocumentDataSerializerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Base class for the document API classes. |
21
|
|
|
* |
22
|
|
|
* @author Christian Flothmann <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class DocumentApiClient extends ApiClient |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var DocumentDataSerializerInterface |
28
|
|
|
*/ |
29
|
|
|
protected $documentDataSerializer; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param HandlerInterface $requestHandler The HTTP request handler |
33
|
|
|
* @param string $version The xAPI version |
34
|
|
|
* @param DocumentDataSerializerInterface $documentDataSerializer The document data serializer |
35
|
|
|
*/ |
36
|
|
|
public function __construct(HandlerInterface $requestHandler, $version, DocumentDataSerializerInterface $documentDataSerializer) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($requestHandler, $version); |
39
|
|
|
|
40
|
|
|
$this->documentDataSerializer = $documentDataSerializer; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Stores a document. |
45
|
|
|
* |
46
|
|
|
* @param string $method HTTP method to use |
47
|
|
|
* @param string $uri Endpoint URI |
48
|
|
|
* @param array $urlParameters URL parameters |
49
|
|
|
* @param Document $document The document to store |
50
|
|
|
*/ |
51
|
|
|
protected function doStoreDocument($method, $uri, $urlParameters, Document $document) |
52
|
|
|
{ |
53
|
|
|
$request = $this->requestHandler->createRequest( |
54
|
|
|
$method, |
55
|
|
|
$uri, |
56
|
|
|
$urlParameters, |
57
|
|
|
$this->documentDataSerializer->serializeDocumentData($document->getData()) |
58
|
|
|
); |
59
|
|
|
$this->requestHandler->executeRequest($request, array(204)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Deletes a document. |
64
|
|
|
* |
65
|
|
|
* @param string $uri The endpoint URI |
66
|
|
|
* @param array $urlParameters The URL parameters |
67
|
|
|
*/ |
68
|
|
|
protected function doDeleteDocument($uri, array $urlParameters) |
69
|
|
|
{ |
70
|
|
|
$request = $this->requestHandler->createRequest('delete', $uri, $urlParameters); |
71
|
|
|
$this->requestHandler->executeRequest($request, array(204)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns a document. |
76
|
|
|
* |
77
|
|
|
* @param string $uri The endpoint URI |
78
|
|
|
* @param array $urlParameters The URL parameters |
79
|
|
|
* |
80
|
|
|
* @return Document The document |
81
|
|
|
*/ |
82
|
|
|
protected function doGetDocument($uri, array $urlParameters) |
83
|
|
|
{ |
84
|
|
|
$request = $this->requestHandler->createRequest('get', $uri, $urlParameters); |
85
|
|
|
$response = $this->requestHandler->executeRequest($request, array(200)); |
86
|
|
|
$document = $this->deserializeDocument($response->getBody(true)); |
87
|
|
|
|
88
|
|
|
return $document; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Deserializes the data of a document. |
93
|
|
|
* |
94
|
|
|
* @param string $data The serialized document data |
95
|
|
|
* |
96
|
|
|
* @return DocumentData The parsed document data |
97
|
|
|
*/ |
98
|
|
|
protected function deserializeDocument($data) |
99
|
|
|
{ |
100
|
|
|
return $this->documentDataSerializer->deserializeDocumentData($data); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|