You are here

public function UserDeveloperConverter::convertUser in Apigee Edge 8

Same name in this branch
  1. 8 src/UserDeveloperConverter.php \Drupal\apigee_edge\UserDeveloperConverter::convertUser()
  2. 8 tests/modules/apigee_edge_test/src/UserDeveloperConverter.php \Drupal\apigee_edge_test\UserDeveloperConverter::convertUser()

Converts Drupal user entity to a developer entity.

Creates a new developer entity if it did not exist for a user or update properties of the existing developer entity.

It modifies only those properties that changed.

Parameters

\Drupal\user\UserInterface $user: The Drupal user entity.

Return value

\Drupal\apigee_edge\Structure\UserToDeveloperConversionResult The result of the conversion.

Overrides UserDeveloperConverterInterface::convertUser

1 call to UserDeveloperConverter::convertUser()
UserDeveloperConverter::convertUser in tests/modules/apigee_edge_test/src/UserDeveloperConverter.php
Push but do not sync Drupal user roles to Apigee Edge.
1 method overrides UserDeveloperConverter::convertUser()
UserDeveloperConverter::convertUser in tests/modules/apigee_edge_test/src/UserDeveloperConverter.php
Push but do not sync Drupal user roles to Apigee Edge.

File

src/UserDeveloperConverter.php, line 91

Class

UserDeveloperConverter
Default user-developer converter service implementation.

Namespace

Drupal\apigee_edge

Code

public function convertUser(UserInterface $user) : UserToDeveloperConversionResult {
  $problems = [];
  $successful_changes = 0;
  $email = isset($user->original) ? $user->original
    ->getEmail() : $user
    ->getEmail();
  $developer = $this->entityTypeManager
    ->getStorage('developer')
    ->load($email);
  if (!$developer) {

    /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
    $developer = $this->entityTypeManager
      ->getStorage('developer')
      ->create([]);
    $developer
      ->setOwnerId($user
      ->id());
  }

  // Synchronise values of base fields.
  foreach (static::DEVELOPER_PROP_USER_BASE_FIELD_MAP as $developer_prop => $base_field) {
    $setter = 'set' . ucfirst($developer_prop);
    $getter = 'get' . ucfirst($developer_prop);

    // Default value for firstname lastname if null.
    if ($user
      ->get($base_field)->value === NULL && ($base_field === "first_name" || $base_field === "last_name")) {
      $base_field_value = $developer
        ->{$getter}() !== NULL ? $developer
        ->{$getter}() : ucfirst($developer_prop);
      $user
        ->set($base_field, $base_field_value);
    }
    if ($user
      ->get($base_field)->value !== $developer
      ->{$getter}()) {
      $developer
        ->{$setter}($user
        ->get($base_field)->value);
      $successful_changes++;
    }
  }

  // Synchronise statuses.
  if ($user
    ->isActive() && $developer
    ->getStatus() === DeveloperInterface::STATUS_INACTIVE) {
    $developer
      ->setStatus(DeveloperInterface::STATUS_ACTIVE);
    $successful_changes++;
  }
  elseif (!$user
    ->isActive() && $developer
    ->getStatus() === DeveloperInterface::STATUS_ACTIVE) {
    $developer
      ->setStatus(DeveloperInterface::STATUS_INACTIVE);
    $successful_changes++;
  }
  foreach ($this->configFactory
    ->get('apigee_edge.sync')
    ->get('user_fields_to_sync') as $field_name) {
    $field_definition = $user
      ->getFieldDefinition($field_name);

    // If the field does not exist, then skip it.
    $attribute_name = $this->fieldAttributeConverter
      ->getAttributeName($field_name);
    if (!isset($field_definition)) {
      $problems[] = new UserDeveloperConversionUserFieldDoesNotExistException($field_name);
      continue;
    }
    $field_type = $field_definition
      ->getType();
    $formatter = $this->fieldStorageFormatManager
      ->lookupPluginForFieldType($field_type);

    // If there is no available storage formatter for the field, then skip it.
    if (!isset($formatter)) {
      $problems[] = new UserDeveloperConversionNoStorageFormatterFoundException($field_definition);
      continue;
    }
    $formatted_field_value = $formatter
      ->encode($user
      ->get($field_name)
      ->getValue());

    // Do not apply unnecessary changes.
    if ($developer
      ->isNew() || $developer
      ->getAttributeValue($attribute_name) !== $formatted_field_value) {

      // Do not leave empty attributes on developers because Apigee Edge
      // Management UI does not like them. (It does not allow to save
      // entities with empty attribute values.)
      if ($formatted_field_value === '') {
        $developer
          ->deleteAttribute($attribute_name);
      }
      else {
        $developer
          ->setAttribute($attribute_name, $formatted_field_value);
      }
      $successful_changes++;
    }
  }
  return new UserToDeveloperConversionResult($developer, $successful_changes, $problems);
}