CrmCoreUserSyncRelation.php 4.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

namespace Drupal\crm_core_user_sync;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\Entity\Relation;
use Drupal\user\UserInterface;

/**
12 13 14
 * Relation service.
 *
 * @package Drupal\crm_core_user_sync
15
 */
16
class CrmCoreUserSyncRelation implements CrmCoreUserSyncRelationInterface {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

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

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

  /**
   * 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.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CrmCoreUserSyncRelationRules $rules) {
    $this->storage = $entity_type_manager->getStorage('crm_core_user_sync_relation');
    $this->rules = $rules;
  }

  /**
46
   * {@inheritdoc}
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   */
  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;
  }

  /**
67
   * {@inheritdoc}
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
   */
  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;
  }

  /**
88
   * {@inheritdoc}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
   */
  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;
  }

  /**
104
   * {@inheritdoc}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
   */
  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;
  }

  /**
120
   * {@inheritdoc}
121 122 123 124 125 126 127 128
   */
  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)) {
129 130 131 132 133

      // Account already have related contact.
      if ($this->getUserIndividualId($account->id())) {
        return NULL;
      }
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
      // Get corresponding contact type.
      $contact_type = $this->rules->getContactType($account);

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

      // Create the contact.
      $contact = Individual::create(array('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();

    \Drupal::logger('crm_core_user_sync')->notice('User @user has been synchronized to the contact @contact_id, relation @rid has been created.', [
      '@user' => $account->getDisplayName(),
      '@contact_id' => $contact->id(),
      '@rid' => $relation->id(),
    ]);

    return $contact;
  }

}