You are here

public function DefaultMatchingEngine::match in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine::match()
  2. 8.2 modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php \Drupal\crm_core_match\Plugin\crm_core_match\engine\DefaultMatchingEngine::match()

Finds matches for given contact.

Parameters

\Drupal\crm_core_contact\ContactInterface $contact: A contact entity used to pass data for identifying a match.

Return value

int[] An array of entity ids for potential matches.

Overrides MatchEngineInterface::match

File

modules/crm_core_match/src/Plugin/crm_core_match/engine/DefaultMatchingEngine.php, line 77

Class

DefaultMatchingEngine
DefaultMatchingEngine class.

Namespace

Drupal\crm_core_match\Plugin\crm_core_match\engine

Code

public function match(ContactInterface $contact) {
  $ids = [];
  $fields = $contact
    ->getFieldDefinitions();
  $results = [];
  $configuration_rules = $this
    ->getConfigurationItem('rules') ?: [];
  foreach ($configuration_rules as $name => $rules) {
    if (isset($fields[$name])) {
      $rules['field'] = $fields[$name];
      if (!$this->pluginManager
        ->hasDefinition($rules['field']
        ->getType())) {
        continue;
      }

      /* @var \Drupal\crm_core_match\Plugin\crm_core_match\field\FieldHandlerInterface $field_handler */
      $field_handler = $this->pluginManager
        ->createInstance($rules['field']
        ->getType(), $rules);
      foreach ($field_handler
        ->getPropertyNames() as $name) {
        $result = $field_handler
          ->match($contact, $name);
        $keys = array_keys($result);
        $key = reset($keys);
        if (isset($results[$key])) {
          if (isset($results[$key][$name])) {
            $results[$key][$name] += $result[$key][$name];
          }
          else {
            $results[$key] += $result[$key];
          }
        }
        else {
          $results += $result;
        }
      }
    }
  }
  foreach ($results as $id => $rule_matches) {
    $total_score = array_sum($rule_matches);
    if ($total_score >= $this
      ->getConfigurationItem('threshold')) {
      $ids[] = $id;
    }
  }
  return $ids;
}