Completed
Push — master ( 2eeb7e...97e31e )
by Christian
02:59
created

StatementsApiClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 5
crap 1
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\IRL;
16
use Xabbuh\XApi\Model\StatementId;
17
use Xabbuh\XApi\Serializer\ActorSerializerInterface;
18
use Xabbuh\XApi\Serializer\StatementResultSerializerInterface;
19
use Xabbuh\XApi\Serializer\StatementSerializerInterface;
20
use Xabbuh\XApi\Model\Actor;
21
use Xabbuh\XApi\Model\Statement;
22
use Xabbuh\XApi\Model\StatementResult;
23
use Xabbuh\XApi\Model\StatementsFilter;
24
25
/**
26
 * Client to access the statements API of an xAPI based learning record store.
27
 *
28
 * @author Christian Flothmann <[email protected]>
29
 */
30
class StatementsApiClient extends ApiClient implements StatementsApiClientInterface
31
{
32
    /**
33
     * @var StatementSerializerInterface
34
     */
35
    private $statementSerializer;
36
37
    /**
38
     * @var StatementResultSerializerInterface
39
     */
40
    private $statementResultSerializer;
41
42
    /**
43
     * @var ActorSerializerInterface
44
     */
45
    private $actorSerializer;
46
47
    /**
48
     * @param HandlerInterface                   $requestHandler            The HTTP request handler
49
     * @param string                             $version                   The xAPI version
50
     * @param StatementSerializerInterface       $statementSerializer       The statement serializer
51
     * @param StatementResultSerializerInterface $statementResultSerializer The statement result serializer
52
     * @param ActorSerializerInterface           $actorSerializer           The actor serializer
53
     */
54 18
    public function __construct(
55
        HandlerInterface $requestHandler,
56
        $version,
57
        StatementSerializerInterface $statementSerializer,
58
        StatementResultSerializerInterface $statementResultSerializer,
59
        ActorSerializerInterface $actorSerializer
60
    ) {
61 18
        parent::__construct($requestHandler, $version);
62 18
        $this->statementSerializer = $statementSerializer;
63 18
        $this->statementResultSerializer = $statementResultSerializer;
64 18
        $this->actorSerializer = $actorSerializer;
65 18
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 4
    public function storeStatement(Statement $statement)
71
    {
72 4
        if (null !== $statement->getId()) {
73 2
            return $this->doStoreStatements(
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doStoreStatements...d()->getValue()), 204); of type array|Xabbuh\XApi\Model\Statement adds the type array to the return on line 73 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...terface::storeStatement of type Xabbuh\XApi\Model\Statement.
Loading history...
74
                $statement,
75 2
                'put',
76 2
                array('statementId' => $statement->getId()->getValue()),
77 2
                204
78
            );
79
        } else {
80 2
            return $this->doStoreStatements($statement);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doStoreStatements($statement); of type array|Xabbuh\XApi\Model\Statement adds the type array to the return on line 80 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...terface::storeStatement of type Xabbuh\XApi\Model\Statement.
Loading history...
81
        }
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 4
    public function storeStatements(array $statements)
88
    {
89
        // check that only Statements without ids will be sent to the LRS
90 4
        foreach ($statements as $statement) {
91
            /** @var Statement $statement */
92
93 4
            $isStatement = is_object($statement) && $statement instanceof Statement;
94
95 4
            if (!$isStatement || null !== $statement->getId()) {
96 4
                throw new \InvalidArgumentException('API can only handle statements without ids');
97
            }
98
        }
99
100 1
        return $this->doStoreStatements($statements);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doStoreStatements($statements); of type array|Xabbuh\XApi\Model\Statement adds the type Xabbuh\XApi\Model\Statement to the return on line 100 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...erface::storeStatements of type Xabbuh\XApi\Model\Statement[].
Loading history...
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 1
    public function voidStatement(Statement $statement, Actor $actor)
107
    {
108 1
        return $this->storeStatement($statement->getVoidStatement($actor));
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 2
    public function getStatement(StatementId $statementId)
115
    {
116 2
        return $this->doGetStatements('statements', array('statementId' => $statementId->getValue()));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements('...tementId->getValue())); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\StatementResult to the return on line 116 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...Interface::getStatement of type Xabbuh\XApi\Model\Statement.
Loading history...
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122 2
    public function getVoidedStatement(StatementId $statementId)
123
    {
124 2
        return $this->doGetStatements('statements', array('voidedStatementId' => $statementId->getValue()));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements('...tementId->getValue())); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\StatementResult to the return on line 124 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...ace::getVoidedStatement of type Xabbuh\XApi\Model\Statement.
Loading history...
125
    }
126
127
    /**
128
     * {@inheritDoc}
129
     */
130 4
    public function getStatements(StatementsFilter $filter = null)
131
    {
132 4
        $urlParameters = array();
133
134 4
        if (null !== $filter) {
135 3
            $urlParameters = $filter->getFilter();
136
        }
137
138
        // the Agent must be JSON encoded
139 4
        if (isset($urlParameters['agent'])) {
140 1
            $urlParameters['agent'] = $this->actorSerializer->serializeActor($urlParameters['agent']);
141
        }
142
143 4
        return $this->doGetStatements('statements', $urlParameters);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements('...ents', $urlParameters); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\Statement to the return on line 143 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...nterface::getStatements of type Xabbuh\XApi\Model\StatementResult.
Loading history...
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 1
    public function getNextStatements(StatementResult $statementResult)
150
    {
151 1
        return $this->doGetStatements($statementResult->getMoreUrlPath()->getValue());
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements($...UrlPath()->getValue()); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\Statement to the return on line 151 which is incompatible with the return type declared by the interface Xabbuh\XApi\Client\Api\S...face::getNextStatements of type Xabbuh\XApi\Model\StatementResult.
Loading history...
152
    }
153
154
    /**
155
     * @param Statement|Statement[] $statements
156
     * @param string                $method
157
     * @param string[]              $parameters
158
     * @param int                   $validStatusCode
159
     *
160
     * @return Statement|Statement[] The created statement(s)
161
     */
162 5
    private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200)
163
    {
164 5
        if (is_array($statements)) {
165 1
            $serializedStatements = $this->statementSerializer->serializeStatements($statements);
166
        } else {
167 4
            $serializedStatements = $this->statementSerializer->serializeStatement($statements);
168
        }
169
170 5
        $request = $this->requestHandler->createRequest(
171
            $method,
172 5
            'statements',
173
            $parameters,
174
            $serializedStatements
175
        );
176 5
        $response = $this->requestHandler->executeRequest($request, array($validStatusCode));
177 5
        $statementIds = json_decode($response->getBody(true));
178
179 5
        if (is_array($statements)) {
180
            /** @var Statement[] $statements */
181 1
            $createdStatements = array();
182
183 1
            foreach ($statements as $index => $statement) {
184 1
                $createdStatements[] = $statement->withId(StatementId::fromString($statementIds[$index]));
185
            }
186
187 1
            return $createdStatements;
188
        } else {
189
            /** @var Statement $statements */
190
191 4
            if (200 === $validStatusCode) {
192 2
                return $statements->withId(StatementId::fromString($statementIds[0]));
193
            } else {
194 2
                return $statements;
195
            }
196
        }
197
    }
198
199
    /**
200
     * Fetch one or more Statements.
201
     *
202
     * @param string $url           URL to request
203
     * @param array  $urlParameters URL parameters
204
     *
205
     * @return Statement|StatementResult
206
     */
207 9
    private function doGetStatements($url, array $urlParameters = array())
208
    {
209 9
        $request = $this->requestHandler->createRequest('get', $url, $urlParameters);
210 9
        $response = $this->requestHandler->executeRequest($request, array(200));
211
212 7
        if (isset($urlParameters['statementId']) || isset($urlParameters['voidedStatementId'])) {
213 2
            return $this->statementSerializer->deserializeStatement($response->getBody(true));
214
        } else {
215 5
            return $this->statementResultSerializer->deserializeStatementResult($response->getBody(true));
216
        }
217
    }
218
}
219