StatementsApiClient::doStoreStatements()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 54
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 54
rs 8.7449
cc 5
eloc 37
nc 8
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Serializer\ActorSerializerInterface;
16
use Xabbuh\XApi\Serializer\StatementResultSerializerInterface;
17
use Xabbuh\XApi\Serializer\StatementSerializerInterface;
18
use Xabbuh\XApi\Model\Actor;
19
use Xabbuh\XApi\Model\Statement;
20
use Xabbuh\XApi\Model\StatementResult;
21
use Xabbuh\XApi\Model\StatementsFilter;
22
23
/**
24
 * Client to access the statements API of an xAPI based learning record store.
25
 *
26
 * @author Christian Flothmann <[email protected]>
27
 */
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
    public function __construct(
53
        HandlerInterface $requestHandler,
54
        $version,
55
        StatementSerializerInterface $statementSerializer,
56
        StatementResultSerializerInterface $statementResultSerializer,
57
        ActorSerializerInterface $actorSerializer
58
    ) {
59
        parent::__construct($requestHandler, $version);
60
        $this->statementSerializer = $statementSerializer;
61
        $this->statementResultSerializer = $statementResultSerializer;
62
        $this->actorSerializer = $actorSerializer;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function storeStatement(Statement $statement)
69
    {
70
        if (null !== $statement->getId()) {
71
            return $this->doStoreStatements(
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doStoreStatements...tement->getId()), 204); of type array|Xabbuh\XApi\Model\Statement adds the type array to the return on line 71 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...
72
                $statement,
73
                'put',
74
                array('statementId' => $statement->getId()),
75
                204
76
            );
77
        } else {
78
            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 78 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...
79
        }
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    public function storeStatements(array $statements)
86
    {
87
        // check that only Statements without ids will be sent to the LRS
88
        foreach ($statements as $statement) {
89
            /** @var Statement $statement */
90
91
            $isStatement = is_object($statement) && $statement instanceof Statement;
92
93
            if (!$isStatement || null !== $statement->getId()) {
94
                throw new \InvalidArgumentException('API can only handle statements without ids');
95
            }
96
        }
97
98
        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 98 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...
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     */
104
    public function voidStatement(Statement $statement, Actor $actor)
105
    {
106
        return $this->storeStatement($statement->getVoidStatement($actor));
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function getStatement($statementId)
113
    {
114
        return $this->doGetStatements('statements', array('statementId' => $statementId));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements('...tId' => $statementId)); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\StatementResult to the return on line 114 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...
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    public function getVoidedStatement($statementId)
121
    {
122
        return $this->doGetStatements('statements', array('voidedStatementId' => $statementId));
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->doGetStatements('...tId' => $statementId)); of type Xabbuh\XApi\Model\Statem...i\Model\StatementResult adds the type Xabbuh\XApi\Model\StatementResult to the return on line 122 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...
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128
    public function getStatements(StatementsFilter $filter = null)
129
    {
130
        $urlParameters = array();
131
132
        if (null !== $filter) {
133
            $urlParameters = $filter->getFilter();
134
        }
135
136
        // the Agent must be JSON encoded
137
        if (isset($urlParameters['agent'])) {
138
            $urlParameters['agent'] = $this->actorSerializer->serializeActor($urlParameters['agent']);
139
        }
140
141
        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 141 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...
142
    }
143
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function getNextStatements(StatementResult $statementResult)
148
    {
149
        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 149 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...
150
    }
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
    private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200)
161
    {
162
        if (is_array($statements)) {
163
            $serializedStatements = $this->statementSerializer->serializeStatements($statements);
164
        } else {
165
            $serializedStatements = $this->statementSerializer->serializeStatement($statements);
166
        }
167
168
        $request = $this->requestHandler->createRequest(
169
            $method,
170
            'statements',
171
            $parameters,
172
            $serializedStatements
173
        );
174
        $response = $this->requestHandler->executeRequest($request, array($validStatusCode));
175
        $statementIds = json_decode($response->getBody(true));
176
177
        if (is_array($statements)) {
178
            /** @var Statement[] $statements */
179
            $createdStatements = array();
180
181
            foreach ($statements as $index => $statement) {
182
                $createdStatements[] = new Statement(
183
                    $statementIds[$index],
184
                    $statement->getActor(),
185
                    $statement->getVerb(),
186
                    $statement->getObject(),
187
                    $statement->getResult()
188
                );
189
            }
190
191
            return $createdStatements;
192
        } else {
193
            /** @var Statement $statements */
194
195
            if (200 === $validStatusCode) {
196
                return new Statement(
197
                    $statementIds[0],
198
                    $statements->getActor(),
199
                    $statements->getVerb(),
200
                    $statements->getObject(),
201
                    $statements->getResult()
202
                );
203
            } else {
204
                return new Statement(
205
                    $statements->getId(),
206
                    $statements->getActor(),
207
                    $statements->getVerb(),
208
                    $statements->getObject(),
209
                    $statements->getResult()
210
                );
211
            }
212
        }
213
    }
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
    private function doGetStatements($url, array $urlParameters = array())
224
    {
225
        $request = $this->requestHandler->createRequest('get', $url, $urlParameters);
226
        $response = $this->requestHandler->executeRequest($request, array(200));
227
228
        if (isset($urlParameters['statementId']) || isset($urlParameters['voidedStatementId'])) {
229
            return $this->statementSerializer->deserializeStatement($response->getBody(true));
230
        } else {
231
            return $this->statementResultSerializer->deserializeStatementResult($response->getBody(true));
232
        }
233
    }
234
}
235