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 |
||
| 22 | class Connector implements ConnectorInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The official service URI; can be overridden via the constructor |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | const SERVICE_PRODUCTION_URL = 'https://api.communibase.nl/0.1/'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The API key which is to be used for the api. |
||
| 33 | * Is required to be set via the constructor. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $apiKey; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The url which is to be used for this connector. Defaults to the production url. |
||
| 41 | * Can be set via the constructor. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $serviceUrl; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array of extra headers to send with each request |
||
| 49 | */ |
||
| 50 | private $extraHeaders = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var QueryLogger |
||
| 54 | */ |
||
| 55 | private $logger; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ClientInterface |
||
| 59 | */ |
||
| 60 | private $client; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create a new Communibase Connector instance based on the given api-key and possible serviceUrl |
||
| 64 | * |
||
| 65 | * @param string $apiKey The API key for Communibase |
||
| 66 | * @param string $serviceUrl The Communibase API endpoint; defaults to self::SERVICE_PRODUCTION_URL |
||
| 67 | * @param ClientInterface $client An optional GuzzleHttp Client (or Interface for mocking) |
||
| 68 | */ |
||
| 69 | public function __construct( |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns an array that has all the fields according to the definition in Communibase. |
||
| 81 | * |
||
| 82 | * @param string $entityType |
||
| 83 | * |
||
| 84 | * @return array |
||
| 85 | * |
||
| 86 | * @throws Exception |
||
| 87 | */ |
||
| 88 | public function getTemplate($entityType) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get a single Entity by its id |
||
| 101 | * |
||
| 102 | * @param string $entityType |
||
| 103 | * @param string $id |
||
| 104 | * @param array $params (optional) |
||
| 105 | * @param string|null $version |
||
| 106 | * |
||
| 107 | * @return array entity |
||
| 108 | * |
||
| 109 | * @throws Exception |
||
| 110 | */ |
||
| 111 | public function getById($entityType, $id, array $params = [], $version = null) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get a single Entity by a ref-string |
||
| 128 | * |
||
| 129 | * @todo if the ref is a parent.parent.parent the code would need further improvement. |
||
| 130 | * |
||
| 131 | * @param array $ref |
||
| 132 | * @param array $parentEntity (optional) |
||
| 133 | * |
||
| 134 | * @return array the referred Entity data |
||
| 135 | * |
||
| 136 | * @throws Exception |
||
| 137 | */ |
||
| 138 | public function getByRef(array $ref, array $parentEntity = []) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get an array of entities by their ids |
||
| 181 | * |
||
| 182 | * @param string $entityType |
||
| 183 | * @param array $ids |
||
| 184 | * @param array $params (optional) |
||
| 185 | * |
||
| 186 | * @return array entities |
||
| 187 | * |
||
| 188 | * @throws Exception |
||
| 189 | */ |
||
| 190 | public function getByIds($entityType, array $ids, array $params = []) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get all entities of a certain type |
||
| 216 | * |
||
| 217 | * @param string $entityType |
||
| 218 | * @param array $params (optional) |
||
| 219 | * |
||
| 220 | * @return array|null |
||
| 221 | * |
||
| 222 | * @throws Exception |
||
| 223 | */ |
||
| 224 | public function getAll($entityType, array $params = []) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get result entityIds of a certain search |
||
| 231 | * |
||
| 232 | * @param string $entityType |
||
| 233 | * @param array $selector (optional) |
||
| 234 | * @param array $params (optional) |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | * |
||
| 238 | * @throws Exception |
||
| 239 | */ |
||
| 240 | public function getIds($entityType, array $selector = [], array $params = []) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get the id of an entity based on a search |
||
| 249 | * |
||
| 250 | * @param string $entityType i.e. Person |
||
| 251 | * @param array $selector (optional) i.e. ['firstName' => 'Henk'] |
||
| 252 | * |
||
| 253 | * @return array resultData |
||
| 254 | * |
||
| 255 | * @throws Exception |
||
| 256 | */ |
||
| 257 | public function getId($entityType, array $selector = []) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Call the aggregate endpoint with a given set of pipeline definitions: |
||
| 267 | * E.g. [ |
||
| 268 | * { "$match": { "_id": {"$ObjectId": "52f8fb85fae15e6d0806e7c7"} } }, |
||
| 269 | * { "$unwind": "$participants" }, |
||
| 270 | * { "$group": { "_id": "$_id", "participantCount": { "$sum": 1 } } } |
||
| 271 | * ] |
||
| 272 | * |
||
| 273 | * @see http://docs.mongodb.org/manual/core/aggregation-pipeline/ |
||
| 274 | * |
||
| 275 | * @param $entityType |
||
| 276 | * @param array $pipeline |
||
| 277 | * @return array |
||
| 278 | * |
||
| 279 | * @throws Exception |
||
| 280 | */ |
||
| 281 | public function aggregate($entityType, array $pipeline) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns an array of the history for the entity with the following format: |
||
| 288 | * |
||
| 289 | * <code> |
||
| 290 | * [ |
||
| 291 | * [ |
||
| 292 | * 'updatedBy' => '', // name of the user |
||
| 293 | * 'updatedAt' => '', // a string according to the DateTime::ISO8601 format |
||
| 294 | * '_id' => '', // the ID of the entity which can ge fetched separately |
||
| 295 | * ], |
||
| 296 | * ... |
||
| 297 | * ] |
||
| 298 | * </code> |
||
| 299 | * |
||
| 300 | * @param string $entityType |
||
| 301 | * @param string $id |
||
| 302 | * |
||
| 303 | * @return array |
||
| 304 | * |
||
| 305 | * @throws Exception |
||
| 306 | */ |
||
| 307 | public function getHistory($entityType, $id) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Search for the given entity by optional passed selector/params |
||
| 314 | * |
||
| 315 | * @param string $entityType |
||
| 316 | * @param array $querySelector |
||
| 317 | * @param array $params (optional) |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | * |
||
| 321 | * @throws Exception |
||
| 322 | */ |
||
| 323 | public function search($entityType, array $querySelector, array $params = []) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * This will save an entity in Communibase. When a _id-field is found, this entity will be updated |
||
| 330 | * |
||
| 331 | * NOTE: When updating, depending on the Entity, you may need to include all fields. |
||
| 332 | * |
||
| 333 | * @param string $entityType |
||
| 334 | * @param array $properties - the to-be-saved entity data |
||
| 335 | * |
||
| 336 | * @return array resultData |
||
| 337 | * |
||
| 338 | * @throws Exception |
||
| 339 | */ |
||
| 340 | public function update($entityType, array $properties) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Finalize an invoice by adding an invoiceNumber to it. |
||
| 350 | * Besides, invoice items will receive a 'generalLedgerAccountNumber'. |
||
| 351 | * This number will be unique and sequential within the 'daybook' of the invoice. |
||
| 352 | * |
||
| 353 | * NOTE: this is Invoice specific |
||
| 354 | * |
||
| 355 | * @param string $entityType |
||
| 356 | * @param string $id |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | * |
||
| 360 | * @throws Exception |
||
| 361 | */ |
||
| 362 | public function finalize($entityType, $id) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Delete something from Communibase |
||
| 373 | * |
||
| 374 | * @param string $entityType |
||
| 375 | * @param string $id |
||
| 376 | * |
||
| 377 | * @return array resultData |
||
| 378 | * |
||
| 379 | * @throws Exception |
||
| 380 | */ |
||
| 381 | public function destroy($entityType, $id) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get the binary contents of a file by its ID |
||
| 388 | * |
||
| 389 | * NOTE: for meta-data like filesize and mimetype, one can use the getById()-method. |
||
| 390 | * |
||
| 391 | * @param string $id id string for the file-entity |
||
| 392 | * |
||
| 393 | * @return StreamInterface Binary contents of the file. |
||
| 394 | * Since the stream can be made a string this works like a charm! |
||
| 395 | * |
||
| 396 | * @throws Exception |
||
| 397 | */ |
||
| 398 | public function getBinary($id) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Uploads the contents of the resource (this could be a file handle) to Communibase |
||
| 409 | * |
||
| 410 | * @param StreamInterface $resource |
||
| 411 | * @param string $name |
||
| 412 | * @param string $destinationPath |
||
| 413 | * @param string $id |
||
| 414 | * |
||
| 415 | * @return array|mixed |
||
| 416 | * |
||
| 417 | * @throws \RuntimeException | Exception |
||
| 418 | */ |
||
| 419 | public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '') |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Perform the actual GET |
||
| 457 | * |
||
| 458 | * @param string $path |
||
| 459 | * @param array $params |
||
| 460 | * @param array $data |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | * |
||
| 464 | * @throws Exception |
||
| 465 | */ |
||
| 466 | protected function doGet($path, array $params = null, array $data = null) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Perform the actual POST |
||
| 473 | * |
||
| 474 | * @param string $path |
||
| 475 | * @param array $params |
||
| 476 | * @param array $data |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | * |
||
| 480 | * @throws Exception |
||
| 481 | */ |
||
| 482 | protected function doPost($path, array $params = null, array $data = null) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Perform the actual PUT |
||
| 489 | * |
||
| 490 | * @param string $path |
||
| 491 | * @param array $params |
||
| 492 | * @param array $data |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | * |
||
| 496 | * @throws Exception |
||
| 497 | */ |
||
| 498 | protected function doPut($path, array $params = null, array $data = null) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Perform the actual DELETE |
||
| 505 | * |
||
| 506 | * @param string $path |
||
| 507 | * @param array $params |
||
| 508 | * @param array $data |
||
| 509 | * |
||
| 510 | * @return array |
||
| 511 | * |
||
| 512 | * @throws Exception |
||
| 513 | */ |
||
| 514 | protected function doDelete($path, array $params = null, array $data = null) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Process the request |
||
| 521 | * |
||
| 522 | * @param string $method |
||
| 523 | * @param string $path |
||
| 524 | * @param array $params |
||
| 525 | * @param array $data |
||
| 526 | * |
||
| 527 | * @return array i.e. [success => true|false, [errors => ['message' => 'this is broken', ..]]] |
||
| 528 | * |
||
| 529 | * @throws Exception |
||
| 530 | */ |
||
| 531 | protected function getResult($method, $path, array $params = null, array $data = null) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @param array $params |
||
| 565 | * |
||
| 566 | * @return mixed |
||
| 567 | */ |
||
| 568 | private function preParseParams(array $params) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Parse the Communibase result and if necessary throw an exception |
||
| 597 | * |
||
| 598 | * @param string $response |
||
| 599 | * @param int $httpCode |
||
| 600 | * |
||
| 601 | * @return array |
||
| 602 | * |
||
| 603 | * @throws Exception |
||
| 604 | */ |
||
| 605 | private function parseResult($response, $httpCode) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Error message based on the most recent JSON error. |
||
| 618 | * |
||
| 619 | * @see http://nl1.php.net/manual/en/function.json-last-error.php |
||
| 620 | * |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | private function getLastJsonError() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Verify the given $id is a valid Communibase string according to format |
||
| 639 | * |
||
| 640 | * @param string $id |
||
| 641 | * |
||
| 642 | * @return bool |
||
| 643 | */ |
||
| 644 | public static function isIdValid($id) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Generate a Communibase compatible ID, that consists of: |
||
| 659 | * |
||
| 660 | * a 4-byte timestamp, |
||
| 661 | * a 3-byte machine identifier, |
||
| 662 | * a 2-byte process id, and |
||
| 663 | * a 3-byte counter, starting with a random value. |
||
| 664 | * |
||
| 665 | * @return string |
||
| 666 | */ |
||
| 667 | public static function generateId() |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Add extra headers to be added to each request |
||
| 687 | * |
||
| 688 | * @see http://php.net/manual/en/function.header.php |
||
| 689 | * |
||
| 690 | * @param array $extraHeaders |
||
| 691 | */ |
||
| 692 | public function addExtraHeaders(array $extraHeaders) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param QueryLogger $logger |
||
| 699 | */ |
||
| 700 | public function setQueryLogger(QueryLogger $logger) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @return QueryLogger |
||
| 707 | */ |
||
| 708 | public function getQueryLogger() |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @todo inroduce overloadability of default GuzzleClient settings |
||
| 715 | * @return ClientInterface |
||
| 716 | * |
||
| 717 | * @throws Exception |
||
| 718 | */ |
||
| 719 | protected function getClient() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Perform the actual call to Communibase |
||
| 743 | * |
||
| 744 | * @param string $method |
||
| 745 | * @param array $arguments |
||
| 746 | * |
||
| 747 | * @return \Psr\Http\Message\ResponseInterface |
||
| 748 | * |
||
| 749 | * @throws Exception |
||
| 750 | */ |
||
| 751 | private function call($method, array $arguments) |
||
| 797 | } |
||
| 798 |