Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Move daoName generation so we don't need to pass the variable name #18552

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions CRM/Dedupe/MergeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
Comment on lines +238 to +257
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I just removed a long switch statement like this in #18546. Aside from the $entity being the nonstandard lowercase, is there any reason not to do it this way?

Suggested change
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');
}
return CRM_Core_DAO_AllCoreTables::getFullName(CRM_Core_DAO_AllCoreTables::convertEntityNameToCamel($entity, TRUE));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw yeah - the follow on patches do more by adding extra variables in there

As an aside, I think a switch in it's own function is not bad for readability - it's the long switch in the midst of a function that is

}

}
6 changes: 3 additions & 3 deletions CRM/Dedupe/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like line 1812 above (which defines the now-unused variable $daoName) is no longer needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw - yes - removing that in this PR makes it confllct with the one in the PR template


// If we're deliberately setting this as primary then add the flag
// and remove it from the current primary location (if there is one).
Expand All @@ -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;
Expand All @@ -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);

Expand Down