Minor function simplification in private function
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 4 May 2022 22:34:21 +0000 (10:34 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 4 May 2022 22:34:21 +0000 (10:34 +1200)
The function does 2 different things depending on whether if finds a duplicate
& having it in a sub-function doesn't make the parent class clearer....

CRM/Contact/BAO/Contact/Utils.php

index fbeeea0c146feb37dd17c6ddac9f7c70b444c607..02fc76b69b5d865cbcd6480cb4c2fb509f9ec620 100644 (file)
@@ -272,9 +272,21 @@ WHERE  id IN ( $idString )
       CRM_Core_Error::deprecatedWarning('attempting to create an employer with invalid contact types is deprecated');
       return;
     }
+    $relationshipIds = [];
     // create employee of relationship
-    [$duplicate, $relationshipIds]
-      = self::legacyCreateMultiple($relationshipTypeID, $employerID, $contactID);
+    $duplicate = CRM_Contact_BAO_Relationship::checkDuplicateRelationship(
+      [
+        'contact_id_a' => $contactID,
+        'contact_id_b' => $employerID,
+        'relationship_type_id' => $relationshipTypeID,
+      ],
+      $contactID,
+      // step 2
+      $employerID
+    );
+    if (!$duplicate) {
+      $relationshipIds = self::legacyCreateMultiple($relationshipTypeID, $employerID, $contactID);
+    }
 
     // In case we change employer, clean previous employer related records.
     if (!$previousEmployerID && !$newContact) {
@@ -332,7 +344,6 @@ WHERE  id IN ( $idString )
    * @param int $contactID
    *
    * @return array
-   * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
   private static function legacyCreateMultiple(int $relationshipTypeID, int $organizationID, int $contactID): array {
@@ -355,24 +366,13 @@ WHERE  id IN ( $idString )
       'relationship_type_id' => $relationshipTypeID,
     ];
 
-    if (
-      CRM_Contact_BAO_Relationship::checkDuplicateRelationship(
-        $contactFields,
-        $contactID,
-        // step 2
-        $organizationID
-      )
-    ) {
-      return [1, []];
-    }
-
     $singleInstanceParams = array_merge($params, $contactFields);
     $relationship = CRM_Contact_BAO_Relationship::add($singleInstanceParams);
     $relationshipIds[] = $relationship->id;
 
     CRM_Contact_BAO_Relationship::addRecent($params, $relationship);
 
-    return [0, $relationshipIds];
+    return $relationshipIds;
   }
 
   /**