1 | <?php |
||
28 | class StatementsApiClient extends ApiClient implements StatementsApiClientInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var StatementSerializerInterface |
||
32 | */ |
||
33 | private $statementSerializer; |
||
34 | |||
35 | /** |
||
36 | * @var StatementResultSerializerInterface |
||
37 | */ |
||
38 | private $statementResultSerializer; |
||
39 | |||
40 | /** |
||
41 | * @var ActorSerializerInterface |
||
42 | */ |
||
43 | private $actorSerializer; |
||
44 | |||
45 | /** |
||
46 | * @param HandlerInterface $requestHandler The HTTP request handler |
||
47 | * @param string $version The xAPI version |
||
48 | * @param StatementSerializerInterface $statementSerializer The statement serializer |
||
49 | * @param StatementResultSerializerInterface $statementResultSerializer The statement result serializer |
||
50 | * @param ActorSerializerInterface $actorSerializer The actor serializer |
||
51 | */ |
||
52 | 18 | public function __construct( |
|
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | 3 | public function storeStatement(Statement $statement) |
|
69 | { |
||
70 | 3 | if (null !== $statement->getId()) { |
|
71 | 2 | return $this->doStoreStatements( |
|
|
|||
72 | $statement, |
||
73 | 2 | 'put', |
|
74 | 2 | array('statementId' => $statement->getId()), |
|
75 | 2 | 204 |
|
76 | ); |
||
77 | } else { |
||
78 | 1 | return $this->doStoreStatements($statement); |
|
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | 4 | public function storeStatements(array $statements) |
|
86 | { |
||
87 | // check that only Statements without ids will be sent to the LRS |
||
88 | 4 | foreach ($statements as $statement) { |
|
89 | /** @var Statement $statement */ |
||
90 | |||
91 | 4 | $isStatement = is_object($statement) && $statement instanceof Statement; |
|
92 | |||
93 | 4 | if (!$isStatement || null !== $statement->getId()) { |
|
94 | 4 | throw new \InvalidArgumentException('API can only handle statements without ids'); |
|
95 | } |
||
96 | } |
||
97 | |||
98 | 1 | return $this->doStoreStatements($statements); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | */ |
||
104 | public function voidStatement(Statement $statement, Actor $actor) |
||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | 2 | public function getStatement($statementId) |
|
116 | |||
117 | /** |
||
118 | * {@inheritDoc} |
||
119 | */ |
||
120 | 2 | public function getVoidedStatement($statementId) |
|
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | 3 | public function getStatements(StatementsFilter $filter = null) |
|
129 | { |
||
130 | 3 | $urlParameters = array(); |
|
131 | |||
132 | 3 | if (null !== $filter) { |
|
133 | 2 | $urlParameters = $filter->getFilter(); |
|
134 | } |
||
135 | |||
136 | // the Agent must be JSON encoded |
||
137 | 3 | if (isset($urlParameters['agent'])) { |
|
138 | $urlParameters['agent'] = $this->actorSerializer->serializeActor($urlParameters['agent']); |
||
139 | } |
||
140 | |||
141 | 3 | return $this->doGetStatements('statements', $urlParameters); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * {@inheritDoc} |
||
146 | */ |
||
147 | 1 | public function getNextStatements(StatementResult $statementResult) |
|
151 | |||
152 | /** |
||
153 | * @param Statement|Statement[] $statements |
||
154 | * @param string $method |
||
155 | * @param string[] $parameters |
||
156 | * @param int $validStatusCode |
||
157 | * |
||
158 | * @return Statement|Statement[] The created statement(s) |
||
159 | */ |
||
160 | 4 | private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200) |
|
214 | |||
215 | /** |
||
216 | * Fetch one or more Statements. |
||
217 | * |
||
218 | * @param string $url URL to request |
||
219 | * @param array $urlParameters URL parameters |
||
220 | * |
||
221 | * @return Statement|StatementResult |
||
222 | */ |
||
223 | 8 | private function doGetStatements($url, array $urlParameters = array()) |
|
234 | } |
||
235 |