You are here

class CrmCoreUserSyncRelationRules in CRM Core 8.3

Same name and namespace in other branches
  1. 8 modules/crm_core_user_sync/src/CrmCoreUserSyncRelationRules.php \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules

CrmCoreUserSyncRelation service.

Hierarchy

Expanded class hierarchy of CrmCoreUserSyncRelationRules

1 file declares its use of CrmCoreUserSyncRelationRules
CrmCoreUserSyncRelationRulesTest.php in modules/crm_core_user_sync/tests/src/Unit/CrmCoreUserSyncRelationRulesTest.php
1 string reference to 'CrmCoreUserSyncRelationRules'
crm_core_user_sync.services.yml in modules/crm_core_user_sync/crm_core_user_sync.services.yml
modules/crm_core_user_sync/crm_core_user_sync.services.yml
1 service uses CrmCoreUserSyncRelationRules
crm_core_user_sync.relation_rules in modules/crm_core_user_sync/crm_core_user_sync.services.yml
Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules

File

modules/crm_core_user_sync/src/CrmCoreUserSyncRelationRules.php, line 12

Namespace

Drupal\crm_core_user_sync
View source
class CrmCoreUserSyncRelationRules {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Rules.
   *
   * @var array
   */
  protected $rules;

  /**
   * Config name.
   *
   * @var string
   */
  private $configName;

  /**
   * Constructs a CrmCoreUserSyncRelationRules object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param string $configName
   *   Name of the configuration object that stores rules.
   */
  public function __construct(ConfigFactoryInterface $config_factory, $configName) {
    $this->configFactory = $config_factory;
    $this->configName = $configName;
  }

  /**
   * Retrieves the individual contact id for specified user.
   *
   * @return int|null
   *   Individual id, if relation exists.
   */
  protected function getRules() {
    if ($this->rules === NULL) {
      $rules = $this->configFactory
        ->get($this->configName)
        ->get('rules');
      uasort($rules, [
        $this,
        'weightCmp',
      ]);
      $this->rules = $rules;
    }
    return $this->rules;
  }

  /**
   * Rules weight comparison function.
   */
  protected function weightCmp(array $a, array $b) {
    if ($a['weight'] == $b['weight']) {
      return 0;
    }
    return $a['weight'] < $b['weight'] ? -1 : 1;
  }

  /**
   * Checks if provided contact can be linked to this account.
   *
   * @param \Drupal\user\UserInterface $account
   *   User  account to check.
   * @param \Drupal\crm_core_contact\IndividualInterface $contact
   *   Contact record to check.
   *
   * @return bool
   *   TRUE if the contact is valid.
   */
  public function valid(UserInterface $account, IndividualInterface $contact) {
    return $contact
      ->bundle() === $this
      ->getContactType($account);
  }

  /**
   * Get contact type resolved from configured synchronization rules.
   *
   * @param \Drupal\user\UserInterface $account
   *   User account to check.
   *
   * @return string|false
   *   Intividual contact type (bundle) to be user for this user account.
   */
  public function getContactType(UserInterface $account) {
    foreach ($this
      ->getRules() as $rule) {
      if ($rule['enabled'] && $account
        ->hasRole($rule['role'])) {
        return $rule['contact_type'];
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CrmCoreUserSyncRelationRules::$configFactory protected property The configuration factory.
CrmCoreUserSyncRelationRules::$configName private property Config name.
CrmCoreUserSyncRelationRules::$rules protected property Rules.
CrmCoreUserSyncRelationRules::getContactType public function Get contact type resolved from configured synchronization rules.
CrmCoreUserSyncRelationRules::getRules protected function Retrieves the individual contact id for specified user.
CrmCoreUserSyncRelationRules::valid public function Checks if provided contact can be linked to this account.
CrmCoreUserSyncRelationRules::weightCmp protected function Rules weight comparison function.
CrmCoreUserSyncRelationRules::__construct public function Constructs a CrmCoreUserSyncRelationRules object.