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 | * @return array entity |
||
| 104 | * |
||
| 105 | * @throws Exception |
||
| 106 | */ |
||
| 107 | public function getById($entityType, $id, array $params = []) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get a single object by a DocumentReference-object. A DocumentReference object looks like |
||
| 121 | * { |
||
| 122 | * rootDocumentId: '524aca8947bd91000600000c', |
||
| 123 | * rootDocumentEntityType: 'Person', |
||
| 124 | * path: [ |
||
| 125 | * { |
||
| 126 | * field: 'addresses', |
||
| 127 | * objectId: '53440792463cda7161000003' |
||
| 128 | * }, ... |
||
| 129 | * ] |
||
| 130 | * } |
||
| 131 | * |
||
| 132 | * @param array $ref |
||
| 133 | * @param array $parentEntity (optional) |
||
| 134 | * |
||
| 135 | * @return array the referred Entity data |
||
| 136 | * |
||
| 137 | * @throws Exception |
||
| 138 | */ |
||
| 139 | public function getByRef($ref, array $parentEntity = []) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get an array of entities by their ids |
||
| 196 | * |
||
| 197 | * @param string $entityType |
||
| 198 | * @param array $ids |
||
| 199 | * @param array $params (optional) |
||
| 200 | * |
||
| 201 | * @return array entities |
||
| 202 | */ |
||
| 203 | public function getByIds($entityType, array $ids, array $params = []) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get all entities of a certain type |
||
| 229 | * |
||
| 230 | * @param string $entityType |
||
| 231 | * @param array $params (optional) |
||
| 232 | * |
||
| 233 | * @return array|null |
||
| 234 | */ |
||
| 235 | public function getAll($entityType, array $params = []) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get result entityIds of a certain search |
||
| 242 | * |
||
| 243 | * @param string $entityType |
||
| 244 | * @param array $selector (optional) |
||
| 245 | * @param array $params (optional) |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | public function getIds($entityType, array $selector = [], array $params = []) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get the id of an entity based on a search |
||
| 258 | * |
||
| 259 | * @param string $entityType i.e. Person |
||
| 260 | * @param array $selector (optional) i.e. ['firstName' => 'Henk'] |
||
| 261 | * |
||
| 262 | * @return array resultData |
||
| 263 | */ |
||
| 264 | public function getId($entityType, array $selector = []) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns an array of the history for the entity with the following format: |
||
| 274 | * |
||
| 275 | * <code> |
||
| 276 | * [ |
||
| 277 | * [ |
||
| 278 | * 'updatedBy' => '', // name of the user |
||
| 279 | * 'updatedAt' => '', // a string according to the DateTime::ISO8601 format |
||
| 280 | * '_id' => '', // the ID of the entity which can ge fetched seperately |
||
| 281 | * ], |
||
| 282 | * ... |
||
| 283 | * ] |
||
| 284 | * </code> |
||
| 285 | * |
||
| 286 | * @param string $entityType |
||
| 287 | * @param string $id |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | * |
||
| 291 | * @throws Exception |
||
| 292 | */ |
||
| 293 | public function getHistory($entityType, $id) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Search for the given entity by optional passed selector/params |
||
| 300 | * |
||
| 301 | * @param string $entityType |
||
| 302 | * @param array $querySelector |
||
| 303 | * @param array $params (optional) |
||
| 304 | * |
||
| 305 | * @return array |
||
| 306 | * |
||
| 307 | * @throws Exception |
||
| 308 | */ |
||
| 309 | public function search($entityType, array $querySelector, array $params = []) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * This will save an entity in Communibase. When a _id-field is found, this entity will be updated |
||
| 316 | * |
||
| 317 | * NOTE: When updating, depending on the Entity, you may need to include all fields. |
||
| 318 | * |
||
| 319 | * @param string $entityType |
||
| 320 | * @param array $properties - the to-be-saved entity data |
||
| 321 | * |
||
| 322 | * @returns array resultData |
||
| 323 | * |
||
| 324 | * @throws Exception |
||
| 325 | */ |
||
| 326 | public function update($entityType, array $properties) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Finalize an invoice by adding an invoiceNumber to it. |
||
| 339 | * Besides, invoice items will receive a "generalLedgerAccountNumber". |
||
| 340 | * This number will be unique and sequential within the "daybook" of the invoice. |
||
| 341 | * |
||
| 342 | * NOTE: this is Invoice specific |
||
| 343 | * |
||
| 344 | * @param string $entityType |
||
| 345 | * @param string $id |
||
| 346 | * |
||
| 347 | * @return array |
||
| 348 | * |
||
| 349 | * @throws Exception |
||
| 350 | */ |
||
| 351 | public function finalize($entityType, $id) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Delete something from Communibase |
||
| 362 | * |
||
| 363 | * @param string $entityType |
||
| 364 | * @param string $id |
||
| 365 | * |
||
| 366 | * @return array resultData |
||
| 367 | */ |
||
| 368 | public function destroy($entityType, $id) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get the binary contents of a file by its ID |
||
| 375 | * |
||
| 376 | * NOTE: for meta-data like filesize and mimetype, one can use the getById()-method. |
||
| 377 | * |
||
| 378 | * @param string $id id string for the file-entity |
||
| 379 | * |
||
| 380 | * @return StreamInterface Binary contents of the file. Since the stream can be made a string this works like a charm! |
||
| 381 | * |
||
| 382 | * @throws Exception |
||
| 383 | */ |
||
| 384 | public function getBinary($id) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Uploads the contents of the resource (this could be a file handle) to Communibase |
||
| 395 | * |
||
| 396 | * @param StreamInterface $resource |
||
| 397 | * @param string $name |
||
| 398 | * @param string $destinationPath |
||
| 399 | * @param string $id |
||
| 400 | * |
||
| 401 | * @return array|mixed |
||
| 402 | * @throws Exception |
||
| 403 | */ |
||
| 404 | public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '') |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $path |
||
| 438 | * @param array $params |
||
| 439 | * @param array $data |
||
| 440 | * |
||
| 441 | * @return array |
||
| 442 | * |
||
| 443 | * @throws Exception |
||
| 444 | */ |
||
| 445 | protected function doGet($path, array $params = null, array $data = null) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $path |
||
| 452 | * @param array $params |
||
| 453 | * @param array $data |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | * |
||
| 457 | * @throws Exception |
||
| 458 | */ |
||
| 459 | protected function doPost($path, array $params = null, array $data = null) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $path |
||
| 466 | * @param array $params |
||
| 467 | * @param array $data |
||
| 468 | * |
||
| 469 | * @return array |
||
| 470 | * |
||
| 471 | * @throws Exception |
||
| 472 | */ |
||
| 473 | protected function doPut($path, array $params = null, array $data = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $path |
||
| 480 | * @param array $params |
||
| 481 | * @param array $data |
||
| 482 | * |
||
| 483 | * @return array |
||
| 484 | * |
||
| 485 | * @throws Exception |
||
| 486 | */ |
||
| 487 | protected function doDelete($path, array $params = null, array $data = null) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Process the request |
||
| 494 | * |
||
| 495 | * @param string $method |
||
| 496 | * @param string $path |
||
| 497 | * @param array $params |
||
| 498 | * @param array $data |
||
| 499 | * |
||
| 500 | * @return array i.e. [success => true|false, [errors => ['message' => 'this is broken', ..]]] |
||
| 501 | * |
||
| 502 | * @throws Exception |
||
| 503 | */ |
||
| 504 | protected function getResult($method, $path, array $params = null, array $data = null) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param array $params |
||
| 534 | * |
||
| 535 | * @return mixed |
||
| 536 | */ |
||
| 537 | private function preParseParams(array $params) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Parse the Communibase result and if necessary throw an exception |
||
| 565 | * |
||
| 566 | * @param string $response |
||
| 567 | * @param int $httpCode |
||
| 568 | * |
||
| 569 | * @return array |
||
| 570 | * |
||
| 571 | * @throws Exception |
||
| 572 | */ |
||
| 573 | private function parseResult($response, $httpCode) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Error message based on the most recent JSON error. |
||
| 586 | * |
||
| 587 | * @see http://nl1.php.net/manual/en/function.json-last-error.php |
||
| 588 | * |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | private function getLastJsonError() |
||
| 604 | |||
| 605 | /** |
||
| 606 | * @param string $id |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | public static function isIdValid($id) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Generate a Communibase compatible ID, that consists of: |
||
| 625 | * |
||
| 626 | * a 4-byte timestamp, |
||
| 627 | * a 3-byte machine identifier, |
||
| 628 | * a 2-byte process id, and |
||
| 629 | * a 3-byte counter, starting with a random value. |
||
| 630 | * |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | public static function generateId() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Add extra headers to be added to each request |
||
| 653 | * |
||
| 654 | * @see http://php.net/manual/en/function.header.php |
||
| 655 | * |
||
| 656 | * @param array $extraHeaders |
||
| 657 | */ |
||
| 658 | public function addExtraHeaders(array $extraHeaders) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * @param QueryLogger $logger |
||
| 665 | */ |
||
| 666 | public function setQueryLogger(QueryLogger $logger) |
||
| 670 | |||
| 671 | /** |
||
| 672 | * @return QueryLogger |
||
| 673 | */ |
||
| 674 | public function getQueryLogger() |
||
| 678 | |||
| 679 | /** |
||
| 680 | * @return \GuzzleHttp\Client |
||
| 681 | * @throws Exception |
||
| 682 | */ |
||
| 683 | protected function getClient() |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $method |
||
| 706 | * @param array $arguments |
||
| 707 | * |
||
| 708 | * @return \Psr\Http\Message\ResponseInterface |
||
| 709 | * @throws Exception |
||
| 710 | */ |
||
| 711 | private function call($method, array $arguments) |
||
| 752 | } |
||
| 753 |