Completed
Push — master ( 105c93...2eeb7e )
by Christian
04:01
created

StatementsApiClient::voidStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
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\StatementId;
16
use Xabbuh\XApi\Serializer\ActorSerializerInterface;
17
use Xabbuh\XApi\Serializer\StatementResultSerializerInterface;
18
use Xabbuh\XApi\Serializer\StatementSerializerInterface;
19
use Xabbuh\XApi\Model\Actor;
20
use Xabbuh\XApi\Model\Statement;
21
use Xabbuh\XApi\Model\StatementResult;
22
use Xabbuh\XApi\Model\StatementsFilter;
23
24
/**
25
 * Client to access the statements API of an xAPI based learning record store.
26
 *
27
 * @author Christian Flothmann <[email protected]>
28
 */
29
class StatementsApiClient extends ApiClient implements StatementsApiClientInterface
30
{
31
    /**
32
     * @var StatementSerializerInterface
33
     */
34
    private $statementSerializer;
35
36
    /**
37
     * @var StatementResultSerializerInterface
38
     */
39
    private $statementResultSerializer;
40
41
    /**
42
     * @var ActorSerializerInterface
43
     */
44
    private $actorSerializer;
45
46
    /**
47
     * @param HandlerInterface                   $requestHandler            The HTTP request handler
48
     * @param string                             $version                   The xAPI version
49
     * @param StatementSerializerInterface       $statementSerializer       The statement serializer
50
     * @param StatementResultSerializerInterface $statementResultSerializer The statement result serializer
51
     * @param ActorSerializerInterface           $actorSerializer           The actor serializer
52
     */
53 18
    public function __construct(
54
        HandlerInterface $requestHandler,
55
        $version,
56
        StatementSerializerInterface $statementSerializer,
57
        StatementResultSerializerInterface $statementResultSerializer,
58
        ActorSerializerInterface $actorSerializer
59
    ) {
60 18
        parent::__construct($requestHandler, $version);
61 18
        $this->statementSerializer = $statementSerializer;
62 18
        $this->statementResultSerializer = $statementResultSerializer;
63 18
        $this->actorSerializer = $actorSerializer;
64 18
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 3
    public function storeStatement(Statement $statement)
70
    {
71 3
        if (null !== $statement->getId()) {
72 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 72 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...
73
                $statement,
74 2
                'put',
75 2
                array('statementId' => $statement->getId()->getValue()),
76 2
                204
77
            );
78
        } else {
79 1
            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 79 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...
80
        }
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86 4
    public function storeStatements(array $statements)
87
    {
88
        // check that only Statements without ids will be sent to the LRS
89 4
        foreach ($statements as $statement) {
90
            /** @var Statement $statement */
91
92 4
            $isStatement = is_object($statement) && $statement instanceof Statement;
93
94 4
            if (!$isStatement || null !== $statement->getId()) {
95 4
                throw new \InvalidArgumentException('API can only handle statements without ids');
96
            }
97
        }
98
99 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 99 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...
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function voidStatement(Statement $statement, Actor $actor)
106
    {
107
        return $this->storeStatement($statement->getVoidStatement($actor));
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 2
    public function getStatement(StatementId $statementId)
114
    {
115 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 115 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...
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 2
    public function getVoidedStatement(StatementId $statementId)
122
    {
123 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 123 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...
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129 2
    public function getStatements(StatementsFilter $filter = null)
130
    {
131 2
        $urlParameters = array();
132
133 2
        if (null !== $filter) {
134 1
            $urlParameters = $filter->getFilter();
135
        }
136
137
        // the Agent must be JSON encoded
138 2
        if (isset($urlParameters['agent'])) {
139
            $urlParameters['agent'] = $this->actorSerializer->serializeActor($urlParameters['agent']);
140
        }
141
142 2
        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 142 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...
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    public function getNextStatements(StatementResult $statementResult)
149
    {
150
        return $this->doGetStatements($statementResult->getMoreUrlPath());
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements($...ult->getMoreUrlPath()); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\Statement to the return on line 150 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...
Documentation introduced by
$statementResult->getMoreUrlPath() is of type object<Xabbuh\XApi\Model\IRL>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
    }
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 4
    private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200)
162
    {
163 4
        if (is_array($statements)) {
164 1
            $serializedStatements = $this->statementSerializer->serializeStatements($statements);
165
        } else {
166 3
            $serializedStatements = $this->statementSerializer->serializeStatement($statements);
167
        }
168
169 4
        $request = $this->requestHandler->createRequest(
170
            $method,
171 4
            'statements',
172
            $parameters,
173
            $serializedStatements
174
        );
175 4
        $response = $this->requestHandler->executeRequest($request, array($validStatusCode));
176 4
        $statementIds = json_decode($response->getBody(true));
177
178 4
        if (is_array($statements)) {
179
            /** @var Statement[] $statements */
180 1
            $createdStatements = array();
181
182 1
            foreach ($statements as $index => $statement) {
183 1
                $createdStatements[] = $statement->withId(StatementId::fromString($statementIds[$index]));
184
            }
185
186 1
            return $createdStatements;
187
        } else {
188
            /** @var Statement $statements */
189
190 3
            if (200 === $validStatusCode) {
191 1
                return $statements->withId(StatementId::fromString($statementIds[0]));
192
            } else {
193 2
                return $statements;
194
            }
195
        }
196
    }
197
198
    /**
199
     * Fetch one or more Statements.
200
     *
201
     * @param string $url           URL to request
202
     * @param array  $urlParameters URL parameters
203
     *
204
     * @return Statement|StatementResult
205
     */
206 6
    private function doGetStatements($url, array $urlParameters = array())
207
    {
208 6
        $request = $this->requestHandler->createRequest('get', $url, $urlParameters);
209 6
        $response = $this->requestHandler->executeRequest($request, array(200));
210
211 4
        if (isset($urlParameters['statementId']) || isset($urlParameters['voidedStatementId'])) {
212 2
            return $this->statementSerializer->deserializeStatement($response->getBody(true));
213
        } else {
214 2
            return $this->statementResultSerializer->deserializeStatementResult($response->getBody(true));
215
        }
216
    }
217
}
218