You are here

class CrmCoreUserSyncRelation in CRM Core 8

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

Relation service.

@package Drupal\crm_core_user_sync

Hierarchy

Expanded class hierarchy of CrmCoreUserSyncRelation

1 string reference to 'CrmCoreUserSyncRelation'
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 CrmCoreUserSyncRelation
crm_core_user_sync.relation in modules/crm_core_user_sync/crm_core_user_sync.services.yml
Drupal\crm_core_user_sync\CrmCoreUserSyncRelation

File

modules/crm_core_user_sync/src/CrmCoreUserSyncRelation.php, line 17

Namespace

Drupal\crm_core_user_sync
View source
class CrmCoreUserSyncRelation implements CrmCoreUserSyncRelationInterface {

  /**
   * Relation storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * Relation rules service.
   *
   * @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules
   */
  protected $rules;

  /**
   * Logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Constructs a CrmCoreUserSyncRelation object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationRules $rules
   *   Relation rules service.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Logger channel.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CrmCoreUserSyncRelationRules $rules, LoggerChannelInterface $logger) {
    $this->storage = $entity_type_manager
      ->getStorage('crm_core_user_sync_relation');
    $this->rules = $rules;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserIndividualId($user_id) {
    $individual_id = NULL;
    $rids = $this->storage
      ->getQuery()
      ->condition('user_id', $user_id)
      ->range(0, 1)
      ->execute();
    if (!empty($rids)) {
      $relation_id = reset($rids);

      /* @var $relation \Drupal\crm_core_user_sync\Entity\Relation */
      $relation = $this->storage
        ->load($relation_id);
      $individual_id = $relation
        ->getIndividualId();
    }
    return $individual_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getIndividualUserId($individual_id) {
    $user_id = NULL;
    $rids = $this->storage
      ->getQuery()
      ->condition('individual_id', $individual_id)
      ->range(0, 1)
      ->execute();
    if (!empty($rids)) {
      $relation_id = reset($rids);

      /* @var $relation \Drupal\crm_core_user_sync\Entity\Relation */
      $relation = $this->storage
        ->load($relation_id);
      $user_id = $relation
        ->getIndividualId();
    }
    return $user_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserRelationId($user_id) {
    $rids = $this->storage
      ->getQuery()
      ->condition('user_id', $user_id)
      ->range(0, 1)
      ->execute();
    if (!empty($rids)) {
      return reset($rids);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getIndividualRelationId($individual_id) {
    $rids = $this->storage
      ->getQuery()
      ->condition('individual_id', $individual_id)
      ->range(0, 1)
      ->execute();
    if (!empty($rids)) {
      return reset($rids);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function relate(UserInterface $account, IndividualInterface $contact = NULL) {

    // No contact and $account->crm_core_no_auto_sync => no sync.
    if (empty($contact) && !empty($account->crm_core_no_auto_sync)) {
      return NULL;
    }
    if (empty($contact)) {

      // Account already have related contact.
      if ($this
        ->getUserIndividualId($account
        ->id())) {
        return NULL;
      }

      // Get corresponding contact type.
      $contact_type = $this->rules
        ->getContactType($account);

      // No rules configured.
      if (!$contact_type) {
        return NULL;
      }

      // Create the contact.
      $contact = Individual::create([
        'type' => $contact_type,
      ]);
      $contact
        ->setOwner($account);

      // For now we just add the name.
      $contact->name->given = $account
        ->getAccountName();
      $contact
        ->save();
    }

    // Check if contact can be synchronized to a contact.
    if (!$this->rules
      ->valid($account, $contact)) {
      return NULL;
    }

    // Check if crm_core_user_sync relation exists for any of endpoint.
    if ($this
      ->getUserIndividualId($account
      ->id()) || $this
      ->getIndividualUserId($contact
      ->id())) {
      return NULL;
    }
    $relation = Relation::create();
    $relation
      ->setUser($account);
    $relation
      ->setIndividual($contact);
    $relation
      ->save();
    $this->logger
      ->notice('User @user @uid has been synchronized to the contact @contact_id, relation @rid has been created.', [
      '@user' => $account
        ->getDisplayName(),
      '@uid' => $account
        ->id(),
      '@contact_id' => $contact
        ->id(),
      '@rid' => $relation
        ->id(),
    ]);
    return $contact;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CrmCoreUserSyncRelation::$logger protected property Logger channel.
CrmCoreUserSyncRelation::$rules protected property Relation rules service.
CrmCoreUserSyncRelation::$storage protected property Relation storage.
CrmCoreUserSyncRelation::getIndividualRelationId public function Retrieves the user id for specified individual contact. Overrides CrmCoreUserSyncRelationInterface::getIndividualRelationId
CrmCoreUserSyncRelation::getIndividualUserId public function Retrieves the user id for specified individual contact. Overrides CrmCoreUserSyncRelationInterface::getIndividualUserId
CrmCoreUserSyncRelation::getUserIndividualId public function Retrieves the individual contact id for specified user. Overrides CrmCoreUserSyncRelationInterface::getUserIndividualId
CrmCoreUserSyncRelation::getUserRelationId public function Retrieves the relation for specified user. Overrides CrmCoreUserSyncRelationInterface::getUserRelationId
CrmCoreUserSyncRelation::relate public function Synchronizes user and contact. Overrides CrmCoreUserSyncRelationInterface::relate
CrmCoreUserSyncRelation::__construct public function Constructs a CrmCoreUserSyncRelation object.