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 |
||
31 | class Connector implements ConnectorInterface |
||
32 | { |
||
33 | /** |
||
34 | * The official service URI; can be overridden via the constructor |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | const SERVICE_PRODUCTION_URL = 'https://api.communibase.nl/0.1/'; |
||
39 | |||
40 | /** |
||
41 | * The API key which is to be used for the api. |
||
42 | * Is required to be set via the constructor. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $apiKey; |
||
47 | |||
48 | /** |
||
49 | * The url which is to be used for this connector. Defaults to the production url. |
||
50 | * Can be set via the constructor. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $serviceUrl; |
||
55 | |||
56 | /** |
||
57 | * @var array of extra headers to send with each request |
||
58 | */ |
||
59 | private $extraHeaders = []; |
||
60 | |||
61 | /** |
||
62 | * @var QueryLogger |
||
63 | */ |
||
64 | private $logger; |
||
65 | |||
66 | /** |
||
67 | * @var ClientInterface |
||
68 | */ |
||
69 | private $client; |
||
70 | |||
71 | /** |
||
72 | * Create a new Communibase Connector instance based on the given api-key and possible serviceUrl |
||
73 | * |
||
74 | * @param string $apiKey The API key for Communibase |
||
75 | * @param string $serviceUrl The Communibase API endpoint; defaults to self::SERVICE_PRODUCTION_URL |
||
76 | * @param ClientInterface $client An optional GuzzleHttp Client (or Interface for mocking) |
||
77 | */ |
||
78 | public function __construct( |
||
87 | |||
88 | /** |
||
89 | * Returns an array that has all the fields according to the definition in Communibase. |
||
90 | * |
||
91 | * @param string $entityType |
||
92 | * |
||
93 | * @return Promise of result |
||
94 | * |
||
95 | * @throws Exception |
||
96 | */ |
||
97 | public function getTemplate($entityType) |
||
98 | { |
||
99 | $params = [ |
||
100 | 'fields' => 'attributes.title', |
||
101 | 'limit' => 1, |
||
102 | ]; |
||
103 | |||
104 | return $this->search('EntityType', ['title' => $entityType], $params)->then(function ($definition) { |
||
105 | return array_fill_keys(array_merge(['_id'], array_column($definition[0]['attributes'], 'title')), null); |
||
106 | }); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get a single Entity by its id |
||
111 | * |
||
112 | * @param string $entityType |
||
113 | * @param string $id |
||
114 | * @param array $params (optional) |
||
115 | * |
||
116 | * @return Promise of result |
||
117 | * |
||
118 | * @throws Exception |
||
119 | */ |
||
120 | public function getById($entityType, $id, array $params = []) |
||
121 | { |
||
122 | if (empty($id)) { |
||
123 | throw new Exception('Id is empty'); |
||
124 | } |
||
125 | if (!$this->isIdValid($id)) { |
||
126 | throw new Exception('Id is invalid, please use a correctly formatted id'); |
||
127 | } |
||
128 | |||
129 | return $this->doGet($entityType . '.json/crud/' . $id, $params); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * NOTE not yet async |
||
134 | * |
||
135 | * Get a single Entity by a ref-string |
||
136 | * |
||
137 | * @param string $ref |
||
138 | * @param array $parentEntity (optional) |
||
139 | * |
||
140 | * @return array the referred Entity data |
||
141 | * |
||
142 | * @throws Exception |
||
143 | */ |
||
144 | public function getByRef($ref, array $parentEntity = []) |
||
162 | |||
163 | /** |
||
164 | * Get an array of entities by their ids |
||
165 | * |
||
166 | * @param string $entityType |
||
167 | * @param array $ids |
||
168 | * @param array $params (optional) |
||
169 | * |
||
170 | * @return Promise of result |
||
171 | */ |
||
172 | public function getByIds($entityType, array $ids, array $params = []) |
||
196 | |||
197 | /** |
||
198 | * Get all entities of a certain type |
||
199 | * |
||
200 | * @param string $entityType |
||
201 | * @param array $params (optional) |
||
202 | * |
||
203 | * @return Promise of result |
||
204 | */ |
||
205 | public function getAll($entityType, array $params = []) |
||
209 | |||
210 | /** |
||
211 | * Get result entityIds of a certain search |
||
212 | * |
||
213 | * @param string $entityType |
||
214 | * @param array $selector (optional) |
||
215 | * @param array $params (optional) |
||
216 | * |
||
217 | * @return Promise of result |
||
218 | */ |
||
219 | public function getIds($entityType, array $selector = [], array $params = []) |
||
227 | |||
228 | /** |
||
229 | * Get the id of an entity based on a search |
||
230 | * |
||
231 | * @param string $entityType i.e. Person |
||
232 | * @param array $selector (optional) i.e. ['firstName' => 'Henk'] |
||
233 | * |
||
234 | * @return Promise of result |
||
235 | */ |
||
236 | public function getId($entityType, array $selector = []) |
||
243 | |||
244 | /** |
||
245 | * Returns an array of the history for the entity with the following format: |
||
246 | * |
||
247 | * <code> |
||
248 | * [ |
||
249 | * [ |
||
250 | * 'updatedBy' => '', // name of the user |
||
251 | * 'updatedAt' => '', // a string according to the DateTime::ISO8601 format |
||
252 | * '_id' => '', // the ID of the entity which can ge fetched seperately |
||
253 | * ], |
||
254 | * ... |
||
255 | * ] |
||
256 | * </code> |
||
257 | * |
||
258 | * @param string $entityType |
||
259 | * @param string $id |
||
260 | * |
||
261 | * @return Promise of result |
||
262 | * |
||
263 | * @throws Exception |
||
264 | */ |
||
265 | public function getHistory($entityType, $id) |
||
269 | |||
270 | /** |
||
271 | * Search for the given entity by optional passed selector/params |
||
272 | * |
||
273 | * @param string $entityType |
||
274 | * @param array $querySelector |
||
275 | * @param array $params (optional) |
||
276 | * |
||
277 | * @return Promise of result |
||
278 | * |
||
279 | * @throws Exception |
||
280 | */ |
||
281 | public function search($entityType, array $querySelector, array $params = []) |
||
285 | |||
286 | /** |
||
287 | * This will save an entity in Communibase. When a _id-field is found, this entity will be updated |
||
288 | * |
||
289 | * NOTE: When updating, depending on the Entity, you may need to include all fields. |
||
290 | * |
||
291 | * @param string $entityType |
||
292 | * @param array $properties - the to-be-saved entity data |
||
293 | * |
||
294 | * @returns Promise of result |
||
295 | * |
||
296 | * @throws Exception |
||
297 | */ |
||
298 | public function update($entityType, array $properties) |
||
308 | |||
309 | /** |
||
310 | * Finalize an invoice by adding an invoiceNumber to it. |
||
311 | * Besides, invoice items will receive a "generalLedgerAccountNumber". |
||
312 | * This number will be unique and sequential within the "daybook" of the invoice. |
||
313 | * |
||
314 | * NOTE: this is Invoice specific |
||
315 | * |
||
316 | * @param string $entityType |
||
317 | * @param string $id |
||
318 | * |
||
319 | * @return Promise of result |
||
320 | * |
||
321 | * @throws Exception |
||
322 | */ |
||
323 | public function finalize($entityType, $id) |
||
331 | |||
332 | /** |
||
333 | * Delete something from Communibase |
||
334 | * |
||
335 | * @param string $entityType |
||
336 | * @param string $id |
||
337 | * |
||
338 | * @return Promise of result |
||
339 | */ |
||
340 | public function destroy($entityType, $id) |
||
344 | |||
345 | /** |
||
346 | * Get the binary contents of a file by its ID |
||
347 | * |
||
348 | * NOTE: for meta-data like filesize and mimetype, one can use the getById()-method. |
||
349 | * |
||
350 | * @param string $id id string for the file-entity |
||
351 | * |
||
352 | * @return StreamInterface Binary contents of the file. Since the stream can be made a string this works like a charm! |
||
353 | * |
||
354 | * @throws Exception |
||
355 | */ |
||
356 | public function getBinary($id) |
||
366 | |||
367 | /** |
||
368 | * Uploads the contents of the resource (this could be a file handle) to Communibase |
||
369 | * |
||
370 | * @param StreamInterface $resource |
||
371 | * @param string $name |
||
372 | * @param string $destinationPath |
||
373 | * @param string $id |
||
374 | * |
||
375 | * @return array|mixed |
||
376 | * @throws Exception |
||
377 | */ |
||
378 | public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '') |
||
410 | |||
411 | /** |
||
412 | * MAGIC for making async sync |
||
413 | * |
||
414 | * @param string $name |
||
415 | * @param array $arguments |
||
416 | * |
||
417 | * @return mixed |
||
418 | */ |
||
419 | public function __call($name, $arguments) |
||
431 | |||
432 | /** |
||
433 | * @param string $path |
||
434 | * @param array $params |
||
435 | * @param array $data |
||
436 | * |
||
437 | * @return Promise |
||
438 | * |
||
439 | * @throws Exception |
||
440 | */ |
||
441 | protected function doGet($path, array $params = null, array $data = null) |
||
445 | |||
446 | /** |
||
447 | * @param string $path |
||
448 | * @param array $params |
||
449 | * @param array $data |
||
450 | * |
||
451 | * @return Promise |
||
452 | * |
||
453 | * @throws Exception |
||
454 | */ |
||
455 | protected function doPost($path, array $params = null, array $data = null) |
||
459 | |||
460 | /** |
||
461 | * @param string $path |
||
462 | * @param array $params |
||
463 | * @param array $data |
||
464 | * |
||
465 | * @return Promise |
||
466 | * |
||
467 | * @throws Exception |
||
468 | */ |
||
469 | protected function doPut($path, array $params = null, array $data = null) |
||
473 | |||
474 | /** |
||
475 | * @param string $path |
||
476 | * @param array $params |
||
477 | * @param array $data |
||
478 | * |
||
479 | * @return Promise |
||
480 | * |
||
481 | * @throws Exception |
||
482 | */ |
||
483 | protected function doDelete($path, array $params = null, array $data = null) |
||
487 | |||
488 | /** |
||
489 | * Process the request |
||
490 | * |
||
491 | * @param string $method |
||
492 | * @param string $path |
||
493 | * @param array $params |
||
494 | * @param array $data |
||
495 | * |
||
496 | * @return Promise array i.e. [success => true|false, [errors => ['message' => 'this is broken', ..]]] |
||
497 | * |
||
498 | * @throws Exception |
||
499 | */ |
||
500 | protected function getResult($method, $path, array $params = null, array $data = null) |
||
539 | |||
540 | /** |
||
541 | * @param array $params |
||
542 | * |
||
543 | * @return mixed |
||
544 | */ |
||
545 | private function preParseParams(array $params) |
||
570 | |||
571 | /** |
||
572 | * Parse the Communibase result and if necessary throw an exception |
||
573 | * |
||
574 | * @param string $response |
||
575 | * @param int $httpCode |
||
576 | * |
||
577 | * @return array |
||
578 | * |
||
579 | * @throws Exception |
||
580 | */ |
||
581 | private function parseResult($response, $httpCode) |
||
591 | |||
592 | /** |
||
593 | * Error message based on the most recent JSON error. |
||
594 | * |
||
595 | * @see http://nl1.php.net/manual/en/function.json-last-error.php |
||
596 | * |
||
597 | * @return string |
||
598 | */ |
||
599 | private function getLastJsonError() |
||
612 | |||
613 | /** |
||
614 | * @param string $id |
||
615 | * |
||
616 | * @return bool |
||
617 | */ |
||
618 | public static function isIdValid($id) |
||
630 | |||
631 | /** |
||
632 | * Generate a Communibase compatible ID, that consists of: |
||
633 | * |
||
634 | * a 4-byte timestamp, |
||
635 | * a 3-byte machine identifier, |
||
636 | * a 2-byte process id, and |
||
637 | * a 3-byte counter, starting with a random value. |
||
638 | * |
||
639 | * @return string |
||
640 | */ |
||
641 | public static function generateId() |
||
658 | |||
659 | /** |
||
660 | * Add extra headers to be added to each request |
||
661 | * |
||
662 | * @see http://php.net/manual/en/function.header.php |
||
663 | * |
||
664 | * @param array $extraHeaders |
||
665 | */ |
||
666 | public function addExtraHeaders(array $extraHeaders) |
||
670 | |||
671 | /** |
||
672 | * @param QueryLogger $logger |
||
673 | */ |
||
674 | public function setQueryLogger(QueryLogger $logger) |
||
678 | |||
679 | /** |
||
680 | * @return QueryLogger |
||
681 | */ |
||
682 | public function getQueryLogger() |
||
686 | |||
687 | /** |
||
688 | * @return \GuzzleHttp\Client |
||
689 | * @throws Exception |
||
690 | */ |
||
691 | protected function getClient() |
||
711 | |||
712 | /** |
||
713 | * @param string $method |
||
714 | * @param array $arguments |
||
715 | * |
||
716 | * @return Promise |
||
717 | * |
||
718 | * @throws Exception |
||
719 | */ |
||
720 | private function call($method, array $arguments) |
||
742 | |||
743 | } |
||
744 |