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 |
||
| 21 | class Connector implements ConnectorInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The official service URI; can be overridden via the constructor |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | const SERVICE_PRODUCTION_URL = 'https://api.communibase.nl/0.1/'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The API key which is to be used for the api. |
||
| 32 | * Is required to be set via the constructor. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $apiKey; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The url which is to be used for this connector. Defaults to the production url. |
||
| 40 | * Can be set via the constructor. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $serviceUrl; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array of extra headers to send with each request |
||
| 48 | */ |
||
| 49 | private $extraHeaders = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var QueryLogger |
||
| 53 | */ |
||
| 54 | private $logger; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ClientInterface |
||
| 58 | */ |
||
| 59 | private $client; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create a new Communibase Connector instance based on the given api-key and possible serviceUrl |
||
| 63 | * |
||
| 64 | * @param string $apiKey The API key for Communibase |
||
| 65 | * @param string $serviceUrl The Communibase API endpoint; defaults to self::SERVICE_PRODUCTION_URL |
||
| 66 | * @param ClientInterface $client An optional GuzzleHttp Client (or Interface for mocking) |
||
| 67 | */ |
||
| 68 | public function __construct( |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns an array that has all the fields according to the definition in Communibase. |
||
| 80 | * |
||
| 81 | * @param string $entityType |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | * |
||
| 85 | * @throws Exception |
||
| 86 | */ |
||
| 87 | public function getTemplate($entityType) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Get a single Entity by its id |
||
| 100 | * |
||
| 101 | * @param string $entityType |
||
| 102 | * @param string $id |
||
| 103 | * @param array $params (optional) |
||
| 104 | * @param string|null $version |
||
| 105 | * |
||
| 106 | * @return array entity |
||
| 107 | * |
||
| 108 | * @throws Exception |
||
| 109 | */ |
||
| 110 | public function getById($entityType, $id, array $params = [], $version = null) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get a single Entity by a ref-string |
||
| 127 | * |
||
| 128 | * @todo if the ref is a parent.parent.parent the code would need further improvement. |
||
| 129 | * |
||
| 130 | * @param array $ref |
||
| 131 | * @param array $parentEntity (optional) |
||
| 132 | * |
||
| 133 | * @return array the referred Entity data |
||
| 134 | * |
||
| 135 | * @throws Exception |
||
| 136 | */ |
||
| 137 | public function getByRef(array $ref, array $parentEntity = []) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get an array of entities by their ids |
||
| 176 | * |
||
| 177 | * @param string $entityType |
||
| 178 | * @param array $ids |
||
| 179 | * @param array $params (optional) |
||
| 180 | * |
||
| 181 | * @return array entities |
||
| 182 | * |
||
| 183 | * @throws Exception |
||
| 184 | */ |
||
| 185 | public function getByIds($entityType, array $ids, array $params = []) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get all entities of a certain type |
||
| 211 | * |
||
| 212 | * @param string $entityType |
||
| 213 | * @param array $params (optional) |
||
| 214 | * |
||
| 215 | * @return array|null |
||
| 216 | * |
||
| 217 | * @throws Exception |
||
| 218 | */ |
||
| 219 | public function getAll($entityType, array $params = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get result entityIds of a certain search |
||
| 226 | * |
||
| 227 | * @param string $entityType |
||
| 228 | * @param array $selector (optional) |
||
| 229 | * @param array $params (optional) |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | * |
||
| 233 | * @throws Exception |
||
| 234 | */ |
||
| 235 | public function getIds($entityType, array $selector = [], array $params = []) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get the id of an entity based on a search |
||
| 244 | * |
||
| 245 | * @param string $entityType i.e. Person |
||
| 246 | * @param array $selector (optional) i.e. ['firstName' => 'Henk'] |
||
| 247 | * |
||
| 248 | * @return array resultData |
||
| 249 | */ |
||
| 250 | public function getId($entityType, array $selector = []) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Call the aggregate endpoint with a given set of pipeline definitions: |
||
| 260 | * E.g. [ |
||
| 261 | * { "$match": { "_id": {"$ObjectId": "52f8fb85fae15e6d0806e7c7"} } }, |
||
| 262 | * { "$unwind": "$participants" }, |
||
| 263 | * { "$group": { "_id": "$_id", "participantCount": { "$sum": 1 } } } |
||
| 264 | * ] |
||
| 265 | * |
||
| 266 | * @see http://docs.mongodb.org/manual/core/aggregation-pipeline/ |
||
| 267 | * |
||
| 268 | * @param $entityType |
||
| 269 | * @param array $pipeline |
||
| 270 | * @return array |
||
| 271 | * |
||
| 272 | * @throws Exception |
||
| 273 | */ |
||
| 274 | public function aggregate($entityType, array $pipeline) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns an array of the history for the entity with the following format: |
||
| 281 | * |
||
| 282 | * <code> |
||
| 283 | * [ |
||
| 284 | * [ |
||
| 285 | * 'updatedBy' => '', // name of the user |
||
| 286 | * 'updatedAt' => '', // a string according to the DateTime::ISO8601 format |
||
| 287 | * '_id' => '', // the ID of the entity which can ge fetched seperately |
||
| 288 | * ], |
||
| 289 | * ... |
||
| 290 | * ] |
||
| 291 | * </code> |
||
| 292 | * |
||
| 293 | * @param string $entityType |
||
| 294 | * @param string $id |
||
| 295 | * |
||
| 296 | * @return array |
||
| 297 | * |
||
| 298 | * @throws Exception |
||
| 299 | */ |
||
| 300 | public function getHistory($entityType, $id) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Search for the given entity by optional passed selector/params |
||
| 307 | * |
||
| 308 | * @param string $entityType |
||
| 309 | * @param array $querySelector |
||
| 310 | * @param array $params (optional) |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | * |
||
| 314 | * @throws Exception |
||
| 315 | */ |
||
| 316 | public function search($entityType, array $querySelector, array $params = []) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * This will save an entity in Communibase. When a _id-field is found, this entity will be updated |
||
| 323 | * |
||
| 324 | * NOTE: When updating, depending on the Entity, you may need to include all fields. |
||
| 325 | * |
||
| 326 | * @param string $entityType |
||
| 327 | * @param array $properties - the to-be-saved entity data |
||
| 328 | * |
||
| 329 | * @returns array resultData |
||
| 330 | * |
||
| 331 | * @throws Exception |
||
| 332 | */ |
||
| 333 | public function update($entityType, array $properties) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Finalize an invoice by adding an invoiceNumber to it. |
||
| 346 | * Besides, invoice items will receive a 'generalLedgerAccountNumber'. |
||
| 347 | * This number will be unique and sequential within the 'daybook' of the invoice. |
||
| 348 | * |
||
| 349 | * NOTE: this is Invoice specific |
||
| 350 | * |
||
| 351 | * @param string $entityType |
||
| 352 | * @param string $id |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | * |
||
| 356 | * @throws Exception |
||
| 357 | */ |
||
| 358 | public function finalize($entityType, $id) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Delete something from Communibase |
||
| 369 | * |
||
| 370 | * @param string $entityType |
||
| 371 | * @param string $id |
||
| 372 | * |
||
| 373 | * @return array resultData |
||
| 374 | * |
||
| 375 | * @throws Exception |
||
| 376 | */ |
||
| 377 | public function destroy($entityType, $id) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the binary contents of a file by its ID |
||
| 384 | * |
||
| 385 | * NOTE: for meta-data like filesize and mimetype, one can use the getById()-method. |
||
| 386 | * |
||
| 387 | * @param string $id id string for the file-entity |
||
| 388 | * |
||
| 389 | * @return StreamInterface Binary contents of the file. Since the stream can be made a string this works like a charm! |
||
| 390 | * |
||
| 391 | * @throws Exception |
||
| 392 | */ |
||
| 393 | public function getBinary($id) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Uploads the contents of the resource (this could be a file handle) to Communibase |
||
| 404 | * |
||
| 405 | * @param StreamInterface $resource |
||
| 406 | * @param string $name |
||
| 407 | * @param string $destinationPath |
||
| 408 | * @param string $id |
||
| 409 | * |
||
| 410 | * @return array|mixed |
||
| 411 | * |
||
| 412 | * @throws \RuntimeException | Exception |
||
| 413 | */ |
||
| 414 | public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '') |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Perform the actual GET |
||
| 452 | * |
||
| 453 | * @param string $path |
||
| 454 | * @param array $params |
||
| 455 | * @param array $data |
||
| 456 | * |
||
| 457 | * @return array |
||
| 458 | * |
||
| 459 | * @throws Exception |
||
| 460 | */ |
||
| 461 | protected function doGet($path, array $params = null, array $data = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Perform the actual POST |
||
| 468 | * |
||
| 469 | * @param string $path |
||
| 470 | * @param array $params |
||
| 471 | * @param array $data |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | * |
||
| 475 | * @throws Exception |
||
| 476 | */ |
||
| 477 | protected function doPost($path, array $params = null, array $data = null) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Perform the actual PUT |
||
| 484 | * |
||
| 485 | * @param string $path |
||
| 486 | * @param array $params |
||
| 487 | * @param array $data |
||
| 488 | * |
||
| 489 | * @return array |
||
| 490 | * |
||
| 491 | * @throws Exception |
||
| 492 | */ |
||
| 493 | protected function doPut($path, array $params = null, array $data = null) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Perform the actual DELETE |
||
| 500 | * |
||
| 501 | * @param string $path |
||
| 502 | * @param array $params |
||
| 503 | * @param array $data |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | * |
||
| 507 | * @throws Exception |
||
| 508 | */ |
||
| 509 | protected function doDelete($path, array $params = null, array $data = null) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Process the request |
||
| 516 | * |
||
| 517 | * @param string $method |
||
| 518 | * @param string $path |
||
| 519 | * @param array $params |
||
| 520 | * @param array $data |
||
| 521 | * |
||
| 522 | * @return array i.e. [success => true|false, [errors => ['message' => 'this is broken', ..]]] |
||
| 523 | * |
||
| 524 | * @throws Exception |
||
| 525 | */ |
||
| 526 | protected function getResult($method, $path, array $params = null, array $data = null) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param array $params |
||
| 556 | * |
||
| 557 | * @return mixed |
||
| 558 | */ |
||
| 559 | private function preParseParams(array $params) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Parse the Communibase result and if necessary throw an exception |
||
| 587 | * |
||
| 588 | * @param string $response |
||
| 589 | * @param int $httpCode |
||
| 590 | * |
||
| 591 | * @return array |
||
| 592 | * |
||
| 593 | * @throws Exception |
||
| 594 | */ |
||
| 595 | private function parseResult($response, $httpCode) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Error message based on the most recent JSON error. |
||
| 608 | * |
||
| 609 | * @see http://nl1.php.net/manual/en/function.json-last-error.php |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | private function getLastJsonError() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Verify the given $id is a valid Communibase string according to format |
||
| 629 | * |
||
| 630 | * @param string $id |
||
| 631 | * |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | public static function isIdValid($id) |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Generate a Communibase compatible ID, that consists of: |
||
| 649 | * |
||
| 650 | * a 4-byte timestamp, |
||
| 651 | * a 3-byte machine identifier, |
||
| 652 | * a 2-byte process id, and |
||
| 653 | * a 3-byte counter, starting with a random value. |
||
| 654 | * |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | public static function generateId() |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Add extra headers to be added to each request |
||
| 677 | * |
||
| 678 | * @see http://php.net/manual/en/function.header.php |
||
| 679 | * |
||
| 680 | * @param array $extraHeaders |
||
| 681 | */ |
||
| 682 | public function addExtraHeaders(array $extraHeaders) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @param QueryLogger $logger |
||
| 689 | */ |
||
| 690 | public function setQueryLogger(QueryLogger $logger) |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @return QueryLogger |
||
| 697 | */ |
||
| 698 | public function getQueryLogger() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return ClientInterface |
||
| 705 | * |
||
| 706 | * @throws Exception |
||
| 707 | */ |
||
| 708 | protected function getClient() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Perform the actual call to Communibase |
||
| 732 | * |
||
| 733 | * @param string $method |
||
| 734 | * @param array $arguments |
||
| 735 | * |
||
| 736 | * @return \Psr\Http\Message\ResponseInterface |
||
| 737 | * |
||
| 738 | * @throws Exception |
||
| 739 | */ |
||
| 740 | private function call($method, array $arguments) |
||
| 785 | } |
||
| 786 |