1 | <?php |
||
31 | final class StatementsApiClient implements StatementsApiClientInterface |
||
32 | { |
||
33 | private $requestHandler; |
||
34 | private $version; |
||
35 | private $statementSerializer; |
||
36 | private $statementResultSerializer; |
||
37 | private $actorSerializer; |
||
38 | |||
39 | /** |
||
40 | * @param HandlerInterface $requestHandler The HTTP request handler |
||
41 | * @param string $version The xAPI version |
||
42 | * @param StatementSerializerInterface $statementSerializer The statement serializer |
||
43 | * @param StatementResultSerializerInterface $statementResultSerializer The statement result serializer |
||
44 | * @param ActorSerializerInterface $actorSerializer The actor serializer |
||
45 | */ |
||
46 | 17 | public function __construct( |
|
47 | HandlerInterface $requestHandler, |
||
48 | $version, |
||
49 | StatementSerializerInterface $statementSerializer, |
||
50 | StatementResultSerializerInterface $statementResultSerializer, |
||
51 | ActorSerializerInterface $actorSerializer |
||
52 | ) { |
||
53 | 17 | $this->requestHandler = $requestHandler; |
|
54 | 17 | $this->version = $version; |
|
55 | 17 | $this->statementSerializer = $statementSerializer; |
|
56 | 17 | $this->statementResultSerializer = $statementResultSerializer; |
|
57 | 17 | $this->actorSerializer = $actorSerializer; |
|
58 | 17 | } |
|
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | 4 | public function storeStatement(Statement $statement) |
|
64 | { |
||
65 | 4 | if (null !== $statement->getId()) { |
|
66 | 2 | return $this->doStoreStatements( |
|
|
|||
67 | $statement, |
||
68 | 2 | 'put', |
|
69 | 2 | array('statementId' => $statement->getId()->getValue()), |
|
70 | 2 | 204 |
|
71 | ); |
||
72 | } else { |
||
73 | 2 | return $this->doStoreStatements($statement); |
|
74 | } |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | 4 | public function storeStatements(array $statements) |
|
81 | { |
||
82 | // check that only Statements without ids will be sent to the LRS |
||
83 | 4 | foreach ($statements as $statement) { |
|
84 | /** @var Statement $statement */ |
||
85 | |||
86 | 4 | $isStatement = is_object($statement) && $statement instanceof Statement; |
|
87 | |||
88 | 4 | if (!$isStatement || null !== $statement->getId()) { |
|
89 | 4 | throw new \InvalidArgumentException('API can only handle statements without ids'); |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 1 | return $this->doStoreStatements($statements); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritDoc} |
||
98 | */ |
||
99 | 1 | public function voidStatement(Statement $statement, Actor $actor) |
|
100 | { |
||
101 | 1 | return $this->storeStatement($statement->getVoidStatement($actor)); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | 2 | public function getStatement(StatementId $statementId, $attachments = true) |
|
108 | { |
||
109 | 2 | return $this->doGetStatements('statements', array( |
|
110 | 2 | 'statementId' => $statementId->getValue(), |
|
111 | 2 | 'attachments' => $attachments ? 'true' : 'false', |
|
112 | )); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | 2 | public function getVoidedStatement(StatementId $statementId, $attachments = true) |
|
119 | { |
||
120 | 2 | return $this->doGetStatements('statements', array( |
|
121 | 2 | 'voidedStatementId' => $statementId->getValue(), |
|
122 | 2 | 'attachments' => $attachments ? 'true' : 'false', |
|
123 | )); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | 4 | public function getStatements(StatementsFilter $filter = null) |
|
144 | |||
145 | /** |
||
146 | * {@inheritDoc} |
||
147 | */ |
||
148 | 1 | public function getNextStatements(StatementResult $statementResult) |
|
152 | |||
153 | /** |
||
154 | * @param Statement|Statement[] $statements |
||
155 | * @param string $method |
||
156 | * @param string[] $parameters |
||
157 | * @param int $validStatusCode |
||
158 | * |
||
159 | * @return Statement|Statement[] The created statement(s) |
||
160 | */ |
||
161 | 5 | private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200) |
|
230 | |||
231 | /** |
||
232 | * Fetch one or more Statements. |
||
233 | * |
||
234 | * @param string $url URL to request |
||
235 | * @param array $urlParameters URL parameters |
||
236 | * |
||
237 | * @return Statement|StatementResult |
||
238 | */ |
||
239 | 9 | private function doGetStatements($url, array $urlParameters = array()) |
|
250 | } |
||
251 |