Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractZohoDao 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 AbstractZohoDao, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class AbstractZohoDao |
||
| 14 | { |
||
| 15 | const ON_DUPLICATE_THROW = 1; |
||
| 16 | const ON_DUPLICATE_MERGE = 2; |
||
| 17 | const MAX_GET_RECORDS = 200; |
||
| 18 | const MAX_GET_RECORDS_BY_ID = 100; |
||
| 19 | const MAX_SIMULTANEOUS_SAVE = 100; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The class implementing API methods not directly related to a specific module. |
||
| 23 | * |
||
| 24 | * @var ZohoClient |
||
| 25 | */ |
||
| 26 | protected $zohoClient; |
||
| 27 | |||
| 28 | public function __construct(ZohoClient $zohoClient) |
||
| 32 | |||
| 33 | abstract protected function getModule(); |
||
| 38 | |||
| 39 | protected $flatFields; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns a flat list of all fields. |
||
| 43 | * |
||
| 44 | * @return array The array of field names for a module |
||
| 45 | */ |
||
| 46 | protected function getFlatFields() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Parse a Zoho Response in order to retrieve one or several ZohoBeans from it. |
||
| 60 | * |
||
| 61 | * @param Response $zohoResponse The response returned by the ZohoClient->call() method |
||
| 62 | * |
||
| 63 | * @return ZohoBeanInterface[] The array of Zoho Beans parsed from the response |
||
| 64 | */ |
||
| 65 | protected function getBeansFromResponse(Response $zohoResponse) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Convert an array of ZohoBeans into a SimpleXMLElement. |
||
| 126 | * |
||
| 127 | * @param $zohoBeans ZohoBeanInterface[] |
||
| 128 | * |
||
| 129 | * @return \SimpleXMLElement The SimpleXMLElement containing the XML for a request |
||
| 130 | */ |
||
| 131 | public function toXml($zohoBeans) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Implements deleteRecords API method. |
||
| 192 | * |
||
| 193 | * @param string $id Zoho Id of the record to delete |
||
| 194 | * |
||
| 195 | * @throws ZohoCRMResponseException |
||
| 196 | */ |
||
| 197 | public function delete($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Implements getRecordById API method. |
||
| 204 | * |
||
| 205 | * @param string|array $id Zoho Id of the record to retrieve OR an array of IDs |
||
| 206 | * |
||
| 207 | * @return ZohoBeanInterface[] The array of Zoho Beans parsed from the response |
||
| 208 | * |
||
| 209 | * @throws ZohoCRMResponseException |
||
| 210 | */ |
||
| 211 | public function getById($id) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Implements getRecords API method. |
||
| 245 | * |
||
| 246 | * @param $sortColumnString |
||
| 247 | * @param $sortOrderString |
||
| 248 | * @param \DateTime $lastModifiedTime |
||
| 249 | * @param $selectColumns |
||
| 250 | * @param $limit |
||
| 251 | * |
||
| 252 | * @return ZohoBeanInterface[] The array of Zoho Beans parsed from the response |
||
| 253 | * |
||
| 254 | * @throws ZohoCRMResponseException |
||
| 255 | */ |
||
| 256 | public function getRecords($sortColumnString = null, $sortOrderString = null, \DateTime $lastModifiedTime = null, $selectColumns = null, $limit = null) |
||
| 257 | { |
||
| 258 | $globalResponse = array(); |
||
| 259 | |||
| 260 | do { |
||
| 261 | try { |
||
| 262 | $fromIndex = count($globalResponse) + 1; |
||
| 263 | $toIndex = $fromIndex + self::MAX_GET_RECORDS - 1; |
||
| 264 | |||
| 265 | if ($limit) { |
||
| 266 | $toIndex = min($limit, $toIndex); |
||
| 267 | } |
||
| 268 | |||
| 269 | $response = $this->zohoClient->getRecords($this->getModule(), $sortColumnString, $sortOrderString, $lastModifiedTime, $selectColumns, $fromIndex, $toIndex); |
||
| 270 | $beans = $this->getBeansFromResponse($response); |
||
| 271 | } catch (ZohoCRMResponseException $e) { |
||
| 272 | // No records found? Let's return an empty array! |
||
| 273 | if ($e->getCode() == 4422) { |
||
| 274 | $beans = array(); |
||
| 275 | } else { |
||
| 276 | throw $e; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | $globalResponse = array_merge($globalResponse, $beans); |
||
| 281 | } while (count($beans) == self::MAX_GET_RECORDS); |
||
| 282 | |||
| 283 | return $globalResponse; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the list of deleted records. |
||
| 288 | * |
||
| 289 | * @param \DateTimeInterface|null $lastModifiedTime |
||
| 290 | * @param int $limit |
||
| 291 | * |
||
| 292 | * @return array |
||
| 293 | * |
||
| 294 | * @throws ZohoCRMResponseException |
||
| 295 | * @throws \Exception |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function getDeletedRecordIds(\DateTimeInterface $lastModifiedTime = null, $limit = null) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Implements getRecords API method. |
||
| 329 | * |
||
| 330 | * @param string $id Zoho Id of the record to delete |
||
| 331 | * @param string $parentModule The parent module of the records |
||
| 332 | * @param int $limit The max number of records to fetch |
||
| 333 | * |
||
| 334 | * @return ZohoBeanInterface[] The array of Zoho Beans parsed from the response |
||
| 335 | * |
||
| 336 | * @throws ZohoCRMResponseException |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function getRelatedRecords($id, $parentModule, $limit = null) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Implements searchRecords API method. |
||
| 370 | * |
||
| 371 | * @param string $searchCondition The search criteria formatted like |
||
| 372 | * @param int $limit The maximum number of beans returned from Zoho |
||
| 373 | * @param \DateTime $lastModifiedTime |
||
| 374 | * @param string $selectColumns The list |
||
| 375 | * |
||
| 376 | * @return ZohoBeanInterface[] The array of Zoho Beans parsed from the response |
||
| 377 | * |
||
| 378 | * @throws ZohoCRMResponseException |
||
| 379 | */ |
||
| 380 | public function searchRecords($searchCondition = null, $limit = null, \DateTime $lastModifiedTime = null, $selectColumns = null) |
||
| 381 | { |
||
| 382 | $globalResponse = array(); |
||
| 383 | |||
| 384 | do { |
||
| 385 | try { |
||
| 386 | $fromIndex = count($globalResponse) + 1; |
||
| 387 | $toIndex = $fromIndex + self::MAX_GET_RECORDS - 1; |
||
| 388 | |||
| 389 | if ($limit) { |
||
| 390 | $toIndex = min($limit - 1, $toIndex); |
||
| 391 | } |
||
| 392 | |||
| 393 | $response = $this->zohoClient->searchRecords($this->getModule(), $searchCondition, $fromIndex, $toIndex, $lastModifiedTime, $selectColumns); |
||
| 394 | $beans = $this->getBeansFromResponse($response); |
||
| 395 | } catch (ZohoCRMResponseException $e) { |
||
| 396 | // No records found? Let's return an empty array! |
||
| 397 | if ($e->getCode() == 4422) { |
||
| 398 | $beans = array(); |
||
| 399 | } else { |
||
| 400 | throw $e; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | $globalResponse = array_merge($globalResponse, $beans); |
||
| 405 | } while (count($beans) == self::MAX_GET_RECORDS); |
||
| 406 | |||
| 407 | return $globalResponse; |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Implements insertRecords API method. |
||
| 412 | * |
||
| 413 | * WARNING : When setting wfTrigger to true, this method will use an API call per bean |
||
| 414 | * passed in argument. This is caused by Zoho limitation which forbids triggering any |
||
| 415 | * workflow when inserting several beans simultaneously. |
||
| 416 | * |
||
| 417 | * @param ZohoBeanInterface[] $beans The Zoho Beans to insert in the CRM |
||
| 418 | * @param bool $wfTrigger Whether or not the call should trigger the workflows related to a "created" event |
||
| 419 | * @param int $duplicateCheck 1 : Throwing error when a duplicate is found; 2 : Merging with existing duplicate |
||
| 420 | * @param bool $isApproval Whether or not to push the record into an approval sandbox first |
||
| 421 | * |
||
| 422 | * @throws ZohoCRMResponseException |
||
| 423 | */ |
||
| 424 | public function insertRecords($beans, $wfTrigger = null, $duplicateCheck = 2, $isApproval = null) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Implements updateRecords API method. |
||
| 468 | * |
||
| 469 | * @param array $beans The list of beans to update. |
||
| 470 | * @param bool $wfTrigger Set value as true to trigger the workflow rule in Zoho |
||
| 471 | * |
||
| 472 | * @return Response The Response object |
||
| 473 | * |
||
| 474 | * @throws ZohoCRMException |
||
| 475 | */ |
||
| 476 | public function updateRecords(array $beans, $wfTrigger = null) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Implements uploadFile API method. |
||
| 530 | * |
||
| 531 | * @param string $id Zoho Id of the record to retrieve |
||
| 532 | * @param string $content The string containing the file |
||
| 533 | * |
||
| 534 | * @return Response The Response object |
||
| 535 | * |
||
| 536 | * @throws ZohoCRMResponseException |
||
| 537 | */ |
||
| 538 | public function uploadFile($id, $content) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Implements downloadFile API method. |
||
| 545 | * |
||
| 546 | * @param string $id unique ID of the attachment |
||
| 547 | * |
||
| 548 | * @return Response The Response object |
||
| 549 | */ |
||
| 550 | public function downloadFile($id) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Saves the bean or array of beans passed in Zoho. |
||
| 557 | * It will perform an insert if the bean has no ZohoID or an update if the bean has a ZohoID. |
||
| 558 | * |
||
| 559 | * @param array|object $beans A bean or an array of beans. |
||
| 560 | * |
||
| 561 | * TODO: isApproval is not used by each module. |
||
| 562 | * TODO: wfTrigger only usable for a single record update/insert. |
||
| 563 | */ |
||
| 564 | public function save($beans, $wfTrigger = false, $duplicateCheck = self::ON_DUPLICATE_MERGE, $isApproval = false) |
||
| 588 | } |
||
| 589 |