Complex classes like StatementsApiClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StatementsApiClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class StatementsApiClient implements StatementsApiClientInterface |
||
31 | { |
||
32 | private $requestHandler; |
||
33 | private $version; |
||
34 | private $statementSerializer; |
||
35 | private $statementResultSerializer; |
||
36 | private $actorSerializer; |
||
37 | |||
38 | /** |
||
39 | * @param HandlerInterface $requestHandler The HTTP request handler |
||
40 | * @param string $version The xAPI version |
||
41 | * @param StatementSerializerInterface $statementSerializer The statement serializer |
||
42 | * @param StatementResultSerializerInterface $statementResultSerializer The statement result serializer |
||
43 | * @param ActorSerializerInterface $actorSerializer The actor serializer |
||
44 | */ |
||
45 | 17 | public function __construct( |
|
58 | |||
59 | /** |
||
60 | * {@inheritDoc} |
||
61 | */ |
||
62 | 4 | public function storeStatement(Statement $statement) |
|
63 | { |
||
64 | 4 | if (null !== $statement->getId()) { |
|
65 | 2 | return $this->doStoreStatements( |
|
|
|||
66 | 2 | $statement, |
|
67 | 2 | 'put', |
|
68 | 2 | array('statementId' => $statement->getId()->getValue()), |
|
69 | 2 | 204 |
|
70 | ); |
||
71 | } else { |
||
72 | 2 | return $this->doStoreStatements($statement); |
|
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * {@inheritDoc} |
||
78 | */ |
||
79 | 4 | public function storeStatements(array $statements) |
|
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | 1 | public function voidStatement(Statement $statement, Actor $actor) |
|
102 | |||
103 | /** |
||
104 | * {@inheritDoc} |
||
105 | */ |
||
106 | 2 | public function getStatement(StatementId $statementId, $attachments = true) |
|
113 | |||
114 | /** |
||
115 | * {@inheritDoc} |
||
116 | */ |
||
117 | 2 | public function getVoidedStatement(StatementId $statementId, $attachments = true) |
|
124 | |||
125 | /** |
||
126 | * {@inheritDoc} |
||
127 | */ |
||
128 | 4 | public function getStatements(StatementsFilter $filter = null, $attachments = true) |
|
129 | { |
||
130 | 4 | $urlParameters = array(); |
|
131 | |||
132 | 4 | if (null !== $filter) { |
|
133 | 3 | $urlParameters = $filter->getFilter(); |
|
134 | } |
||
135 | |||
136 | // the Agent must be JSON encoded |
||
137 | 4 | if (isset($urlParameters['agent'])) { |
|
138 | 1 | $urlParameters['agent'] = $this->actorSerializer->serializeActor($urlParameters['agent']); |
|
139 | } |
||
140 | |||
141 | 4 | 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 | 5 | private function doStoreStatements($statements, $method = 'post', $parameters = array(), $validStatusCode = 200) |
|
161 | { |
||
162 | 5 | $attachments = array(); |
|
163 | |||
164 | 5 | if (is_array($statements)) { |
|
165 | 1 | foreach ($statements as $statement) { |
|
166 | 1 | if (null !== $statement->getAttachments()) { |
|
167 | foreach ($statement->getAttachments() as $attachment) { |
||
168 | if ($attachment->getContent()) { |
||
169 | 1 | $attachments[] = $attachment; |
|
170 | } |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | 1 | $serializedStatements = $this->statementSerializer->serializeStatements($statements); |
|
176 | } else { |
||
177 | 4 | if (null !== $statements->getAttachments()) { |
|
178 | foreach ($statements->getAttachments() as $attachment) { |
||
179 | if ($attachment->getContent()) { |
||
180 | $attachments[] = $attachment; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | |||
185 | 4 | $serializedStatements = $this->statementSerializer->serializeStatement($statements); |
|
186 | } |
||
187 | |||
188 | 5 | $headers = array(); |
|
189 | |||
190 | 5 | if (!empty($attachments)) { |
|
191 | $builder = new MultipartStatementBody($serializedStatements, $attachments); |
||
192 | $headers = array( |
||
193 | 'Content-Type' => 'multipart/mixed; boundary='.$builder->getBoundary(), |
||
194 | ); |
||
195 | $body = $builder->build(); |
||
196 | } else { |
||
197 | 5 | $body = $serializedStatements; |
|
198 | } |
||
199 | |||
200 | 5 | $request = $this->requestHandler->createRequest( |
|
201 | 5 | $method, |
|
202 | 5 | 'statements', |
|
203 | 5 | $parameters, |
|
204 | 5 | $body, |
|
205 | 5 | $headers |
|
206 | ); |
||
207 | 5 | $response = $this->requestHandler->executeRequest($request, array($validStatusCode)); |
|
208 | 5 | $statementIds = json_decode((string) $response->getBody()); |
|
209 | |||
210 | 5 | if (is_array($statements)) { |
|
211 | /** @var Statement[] $statements */ |
||
212 | 1 | $createdStatements = array(); |
|
213 | |||
214 | 1 | foreach ($statements as $index => $statement) { |
|
215 | 1 | $createdStatements[] = $statement->withId(StatementId::fromString($statementIds[$index])); |
|
216 | } |
||
217 | |||
218 | 1 | return $createdStatements; |
|
219 | } else { |
||
220 | /** @var Statement $statements */ |
||
221 | |||
222 | 4 | if (200 === $validStatusCode) { |
|
223 | 2 | return $statements->withId(StatementId::fromString($statementIds[0])); |
|
224 | } else { |
||
225 | 2 | return $statements; |
|
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Fetch one or more Statements. |
||
232 | * |
||
233 | * @param string $url URL to request |
||
234 | * @param array $urlParameters URL parameters |
||
235 | * |
||
236 | * @return Statement|StatementResult |
||
237 | */ |
||
238 | 9 | private function doGetStatements($url, array $urlParameters = array()) |
|
270 | |||
271 | private function parseMultipartResponseBody($body, $boundary) |
||
309 | } |
||
310 |