Complex classes like Connector 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 Connector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Connector implements ConnectorInterface |
||
20 | { |
||
21 | /** |
||
22 | * The official service URI; can be overridden via the constructor |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const SERVICE_PRODUCTION_URL = 'https://api.communibase.nl/0.1/'; |
||
27 | |||
28 | /** |
||
29 | * The API key which is to be used for the api. |
||
30 | * Is required to be set via the constructor. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $apiKey; |
||
35 | |||
36 | /** |
||
37 | * The url which is to be used for this connector. Defaults to the production url. |
||
38 | * Can be set via the constructor. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $serviceUrl; |
||
43 | |||
44 | /** |
||
45 | * @var array of extra headers to send with each request |
||
46 | */ |
||
47 | private $extraHeaders = []; |
||
48 | |||
49 | /** |
||
50 | * @var QueryLogger |
||
51 | */ |
||
52 | private $logger; |
||
53 | |||
54 | /** |
||
55 | * @var ClientInterface |
||
56 | */ |
||
57 | private $client; |
||
58 | |||
59 | /** |
||
60 | * Create a new Communibase Connector instance based on the given api-key and possible serviceUrl |
||
61 | * |
||
62 | * @param string $apiKey The API key for Communibase |
||
63 | * @param string $serviceUrl The Communibase API endpoint; defaults to self::SERVICE_PRODUCTION_URL |
||
64 | * @param ClientInterface $client An optional GuzzleHttp Client (or Interface for mocking) |
||
65 | */ |
||
66 | public function __construct( |
||
75 | |||
76 | /** |
||
77 | * Returns an array that has all the fields according to the definition in Communibase. |
||
78 | * |
||
79 | * @param string $entityType |
||
80 | * |
||
81 | * @return array |
||
82 | * |
||
83 | * @throws Exception |
||
84 | */ |
||
85 | public function getTemplate($entityType) |
||
95 | |||
96 | /** |
||
97 | * Get a single Entity by its id |
||
98 | * |
||
99 | * @param string $entityType |
||
100 | * @param string $id |
||
101 | * @param array $params (optional) |
||
102 | * |
||
103 | * @param string|null $version |
||
104 | * @return array entity |
||
105 | * |
||
106 | * @throws Exception |
||
107 | */ |
||
108 | public function getById($entityType, $id, array $params = [], $version = null) |
||
121 | |||
122 | /** |
||
123 | * Get a single Entity by a ref-string |
||
124 | * |
||
125 | * @param string $ref |
||
126 | * @param array $parentEntity (optional) |
||
127 | * |
||
128 | * @return array the referred Entity data |
||
129 | * |
||
130 | * @throws Exception |
||
131 | */ |
||
132 | public function getByRef($ref, array $parentEntity = []) |
||
150 | |||
151 | /** |
||
152 | * Get an array of entities by their ids |
||
153 | * |
||
154 | * @param string $entityType |
||
155 | * @param array $ids |
||
156 | * @param array $params (optional) |
||
157 | * |
||
158 | * @return array entities |
||
159 | */ |
||
160 | public function getByIds($entityType, array $ids, array $params = []) |
||
183 | |||
184 | /** |
||
185 | * Get all entities of a certain type |
||
186 | * |
||
187 | * @param string $entityType |
||
188 | * @param array $params (optional) |
||
189 | * |
||
190 | * @return array|null |
||
191 | */ |
||
192 | public function getAll($entityType, array $params = []) |
||
196 | |||
197 | /** |
||
198 | * Get result entityIds of a certain search |
||
199 | * |
||
200 | * @param string $entityType |
||
201 | * @param array $selector (optional) |
||
202 | * @param array $params (optional) |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getIds($entityType, array $selector = [], array $params = []) |
||
212 | |||
213 | /** |
||
214 | * Get the id of an entity based on a search |
||
215 | * |
||
216 | * @param string $entityType i.e. Person |
||
217 | * @param array $selector (optional) i.e. ['firstName' => 'Henk'] |
||
218 | * |
||
219 | * @return array resultData |
||
220 | */ |
||
221 | public function getId($entityType, array $selector = []) |
||
228 | |||
229 | /** |
||
230 | * Call the aggregate endpoint with a given set of pipeline definitions: |
||
231 | * E.g. [ |
||
232 | * { "$match": { "_id": {"$ObjectId": "52f8fb85fae15e6d0806e7c7"} } }, |
||
233 | * { "$unwind": "$participants" }, |
||
234 | * { "$group": { "_id": "$_id", "participantCount": { "$sum": 1 } } } |
||
235 | * ] |
||
236 | * |
||
237 | * @see http://docs.mongodb.org/manual/core/aggregation-pipeline/ |
||
238 | * |
||
239 | * @param $entityType |
||
240 | * @param array $pipeline |
||
241 | * @return array |
||
242 | */ |
||
243 | public function aggregate($entityType, array $pipeline) |
||
247 | |||
248 | /** |
||
249 | * Returns an array of the history for the entity with the following format: |
||
250 | * |
||
251 | * <code> |
||
252 | * [ |
||
253 | * [ |
||
254 | * 'updatedBy' => '', // name of the user |
||
255 | * 'updatedAt' => '', // a string according to the DateTime::ISO8601 format |
||
256 | * '_id' => '', // the ID of the entity which can ge fetched seperately |
||
257 | * ], |
||
258 | * ... |
||
259 | * ] |
||
260 | * </code> |
||
261 | * |
||
262 | * @param string $entityType |
||
263 | * @param string $id |
||
264 | * |
||
265 | * @return array |
||
266 | * |
||
267 | * @throws Exception |
||
268 | */ |
||
269 | public function getHistory($entityType, $id) |
||
273 | |||
274 | /** |
||
275 | * Search for the given entity by optional passed selector/params |
||
276 | * |
||
277 | * @param string $entityType |
||
278 | * @param array $querySelector |
||
279 | * @param array $params (optional) |
||
280 | * |
||
281 | * @return array |
||
282 | * |
||
283 | * @throws Exception |
||
284 | */ |
||
285 | public function search($entityType, array $querySelector, array $params = []) |
||
289 | |||
290 | /** |
||
291 | * This will save an entity in Communibase. When a _id-field is found, this entity will be updated |
||
292 | * |
||
293 | * NOTE: When updating, depending on the Entity, you may need to include all fields. |
||
294 | * |
||
295 | * @param string $entityType |
||
296 | * @param array $properties - the to-be-saved entity data |
||
297 | * |
||
298 | * @returns array resultData |
||
299 | * |
||
300 | * @throws Exception |
||
301 | */ |
||
302 | public function update($entityType, array $properties) |
||
312 | |||
313 | /** |
||
314 | * Finalize an invoice by adding an invoiceNumber to it. |
||
315 | * Besides, invoice items will receive a "generalLedgerAccountNumber". |
||
316 | * This number will be unique and sequential within the "daybook" of the invoice. |
||
317 | * |
||
318 | * NOTE: this is Invoice specific |
||
319 | * |
||
320 | * @param string $entityType |
||
321 | * @param string $id |
||
322 | * |
||
323 | * @return array |
||
324 | * |
||
325 | * @throws Exception |
||
326 | */ |
||
327 | public function finalize($entityType, $id) |
||
335 | |||
336 | /** |
||
337 | * Delete something from Communibase |
||
338 | * |
||
339 | * @param string $entityType |
||
340 | * @param string $id |
||
341 | * |
||
342 | * @return array resultData |
||
343 | */ |
||
344 | public function destroy($entityType, $id) |
||
348 | |||
349 | /** |
||
350 | * Get the binary contents of a file by its ID |
||
351 | * |
||
352 | * NOTE: for meta-data like filesize and mimetype, one can use the getById()-method. |
||
353 | * |
||
354 | * @param string $id id string for the file-entity |
||
355 | * |
||
356 | * @return StreamInterface Binary contents of the file. Since the stream can be made a string this works like a charm! |
||
357 | * |
||
358 | * @throws Exception |
||
359 | */ |
||
360 | public function getBinary($id) |
||
368 | |||
369 | /** |
||
370 | * Uploads the contents of the resource (this could be a file handle) to Communibase |
||
371 | * |
||
372 | * @param StreamInterface $resource |
||
373 | * @param string $name |
||
374 | * @param string $destinationPath |
||
375 | * @param string $id |
||
376 | * |
||
377 | * @return array|mixed |
||
378 | * @throws Exception |
||
379 | */ |
||
380 | public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '') |
||
411 | |||
412 | /** |
||
413 | * @param string $path |
||
414 | * @param array $params |
||
415 | * @param array $data |
||
416 | * |
||
417 | * @return array |
||
418 | * |
||
419 | * @throws Exception |
||
420 | */ |
||
421 | protected function doGet($path, array $params = null, array $data = null) |
||
425 | |||
426 | /** |
||
427 | * @param string $path |
||
428 | * @param array $params |
||
429 | * @param array $data |
||
430 | * |
||
431 | * @return array |
||
432 | * |
||
433 | * @throws Exception |
||
434 | */ |
||
435 | protected function doPost($path, array $params = null, array $data = null) |
||
439 | |||
440 | /** |
||
441 | * @param string $path |
||
442 | * @param array $params |
||
443 | * @param array $data |
||
444 | * |
||
445 | * @return array |
||
446 | * |
||
447 | * @throws Exception |
||
448 | */ |
||
449 | protected function doPut($path, array $params = null, array $data = null) |
||
453 | |||
454 | /** |
||
455 | * @param string $path |
||
456 | * @param array $params |
||
457 | * @param array $data |
||
458 | * |
||
459 | * @return array |
||
460 | * |
||
461 | * @throws Exception |
||
462 | */ |
||
463 | protected function doDelete($path, array $params = null, array $data = null) |
||
467 | |||
468 | /** |
||
469 | * Process the request |
||
470 | * |
||
471 | * @param string $method |
||
472 | * @param string $path |
||
473 | * @param array $params |
||
474 | * @param array $data |
||
475 | * |
||
476 | * @return array i.e. [success => true|false, [errors => ['message' => 'this is broken', ..]]] |
||
477 | * |
||
478 | * @throws Exception |
||
479 | */ |
||
480 | protected function getResult($method, $path, array $params = null, array $data = null) |
||
507 | |||
508 | /** |
||
509 | * @param array $params |
||
510 | * |
||
511 | * @return mixed |
||
512 | */ |
||
513 | private function preParseParams(array $params) |
||
538 | |||
539 | /** |
||
540 | * Parse the Communibase result and if necessary throw an exception |
||
541 | * |
||
542 | * @param string $response |
||
543 | * @param int $httpCode |
||
544 | * |
||
545 | * @return array |
||
546 | * |
||
547 | * @throws Exception |
||
548 | */ |
||
549 | private function parseResult($response, $httpCode) |
||
559 | |||
560 | /** |
||
561 | * Error message based on the most recent JSON error. |
||
562 | * |
||
563 | * @see http://nl1.php.net/manual/en/function.json-last-error.php |
||
564 | * |
||
565 | * @return string |
||
566 | */ |
||
567 | private function getLastJsonError() |
||
580 | |||
581 | /** |
||
582 | * @param string $id |
||
583 | * |
||
584 | * @return bool |
||
585 | */ |
||
586 | public static function isIdValid($id) |
||
598 | |||
599 | /** |
||
600 | * Generate a Communibase compatible ID, that consists of: |
||
601 | * |
||
602 | * a 4-byte timestamp, |
||
603 | * a 3-byte machine identifier, |
||
604 | * a 2-byte process id, and |
||
605 | * a 3-byte counter, starting with a random value. |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | public static function generateId() |
||
626 | |||
627 | /** |
||
628 | * Add extra headers to be added to each request |
||
629 | * |
||
630 | * @see http://php.net/manual/en/function.header.php |
||
631 | * |
||
632 | * @param array $extraHeaders |
||
633 | */ |
||
634 | public function addExtraHeaders(array $extraHeaders) |
||
638 | |||
639 | /** |
||
640 | * @param QueryLogger $logger |
||
641 | */ |
||
642 | public function setQueryLogger(QueryLogger $logger) |
||
646 | |||
647 | /** |
||
648 | * @return QueryLogger |
||
649 | */ |
||
650 | public function getQueryLogger() |
||
654 | |||
655 | /** |
||
656 | * @return \GuzzleHttp\Client |
||
657 | * @throws Exception |
||
658 | */ |
||
659 | protected function getClient() |
||
679 | |||
680 | /** |
||
681 | * @param string $method |
||
682 | * @param array $arguments |
||
683 | * |
||
684 | * @return \Psr\Http\Message\ResponseInterface |
||
685 | * @throws Exception |
||
686 | */ |
||
687 | private function call($method, array $arguments) |
||
728 | } |
||
729 |