From 99d57de1d3ffa77d03345187677705dd2c7417f6 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 17 Sep 2020 15:34:30 +1200 Subject: [PATCH] Determine name of DAO when needed rather than passing a variable around --- CRM/Dedupe/MergeHandler.php | 40 +++++++++++++++++++++++++++++++++---- CRM/Dedupe/Merger.php | 6 +++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CRM/Dedupe/MergeHandler.php b/CRM/Dedupe/MergeHandler.php index f67371d6e081..e12cd7519d07 100644 --- a/CRM/Dedupe/MergeHandler.php +++ b/CRM/Dedupe/MergeHandler.php @@ -196,18 +196,19 @@ public function getLocationBlocksToMerge(): array { * This is intended as a refactoring step - not the long term function. Do not * call from any function other than the one it is taken from (Merger::mergeLocations). * - * @param string $daoName * @param int $otherBlockId * @param string $name * @param int $blkCount * - * @return mixed + * @return CRM_Core_DAO_Address|CRM_Core_DAO_Email|CRM_Core_DAO_IM|CRM_Core_DAO_Phone|CRM_Core_DAO_Website + * + * @throws \CRM_Core_Exception */ - public function copyDataToNewBlockDAO(string $daoName, $otherBlockId, $name, $blkCount) { + public function copyDataToNewBlockDAO($otherBlockId, $name, $blkCount) { $locationBlocks = CRM_Dedupe_Merger::getLocationBlockInfo(); $migrationInfo = $this->getMigrationInfo(); // For the block which belongs to other-contact, link the location block to main-contact - $otherBlockDAO = new $daoName(); + $otherBlockDAO = $this->getDAOForLocationEntity($name); $otherBlockDAO->contact_id = $this->getToKeepID(); // Get the ID of this block on the 'other' contact, otherwise skip @@ -225,4 +226,35 @@ public function copyDataToNewBlockDAO(string $daoName, $otherBlockId, $name, $bl return $otherBlockDAO; } + /** + * Get the DAO object appropriate to the location entity. + * + * @param string $entity + * + * @return CRM_Core_DAO_Address|CRM_Core_DAO_Email|CRM_Core_DAO_IM|CRM_Core_DAO_Phone|CRM_Core_DAO_Website + * @throws \CRM_Core_Exception + */ + public function getDAOForLocationEntity($entity) { + switch ($entity) { + case 'email': + return new CRM_Core_DAO_Email(); + + case 'address': + return new CRM_Core_DAO_Address(); + + case 'phone': + return new CRM_Core_DAO_Phone(); + + case 'website': + return new CRM_Core_DAO_Website(); + + case 'im': + return new CRM_Core_DAO_IM(); + + default: + // Mostly here, along with the switch over a more concise format, to help IDEs understand the possibilities. + throw new CRM_Core_Exception('Unsupported entity'); + } + } + } diff --git a/CRM/Dedupe/Merger.php b/CRM/Dedupe/Merger.php index d72c62414586..745d7464e7d6 100644 --- a/CRM/Dedupe/Merger.php +++ b/CRM/Dedupe/Merger.php @@ -1820,7 +1820,7 @@ public static function mergeLocations($mergeHandler) { if (!$otherBlockId) { continue; } - $otherBlockDAO = $mergeHandler->copyDataToNewBlockDAO($daoName, $otherBlockId, $name, $blkCount); + $otherBlockDAO = $mergeHandler->copyDataToNewBlockDAO($otherBlockId, $name, $blkCount); // If we're deliberately setting this as primary then add the flag // and remove it from the current primary location (if there is one). @@ -1829,7 +1829,7 @@ public static function mergeLocations($mergeHandler) { if (!$changePrimary && $set_primary == "1") { $otherBlockDAO->is_primary = 1; if ($primaryDAOId) { - $removePrimaryDAO = new $daoName(); + $removePrimaryDAO = $mergeHandler->getDAOForLocationEntity($name); $removePrimaryDAO->id = $primaryDAOId; $removePrimaryDAO->is_primary = 0; $blocksDAO[$name]['update'][$primaryDAOId] = $removePrimaryDAO; @@ -1848,7 +1848,7 @@ public static function mergeLocations($mergeHandler) { // overwrite - need to delete block which belongs to main-contact. if (!empty($mainBlockId) && $values['is_replace']) { - $deleteDAO = new $daoName(); + $deleteDAO = $mergeHandler->getDAOForLocationEntity($name); $deleteDAO->id = $mainBlockId; $deleteDAO->find(TRUE);