You are here

protected function UserUpdate::beforeUserSave in Apigee Edge 8

Execute actions before the user gets saved.

Parameters

\Drupal\apigee_edge\Structure\DeveloperToUserConversionResult $result: Result of the entity conversion.

Throws

\Exception Can throw an exception to abort user save.

Overrides UserCreateUpdate::beforeUserSave

File

src/Job/UserUpdate.php, line 33

Class

UserUpdate
A job to update a Drupal user based on an Apigee Edge developer.

Namespace

Drupal\apigee_edge\Job

Code

protected function beforeUserSave(DeveloperToUserConversionResult $result) : void {
  parent::beforeUserSave($result);

  /** @var \Drupal\user\UserInterface $original_user */
  $original_user = \Drupal::entityTypeManager()
    ->getStorage('user')
    ->loadUnchanged($result
    ->getUser()
    ->id());

  // Even if the developer has been blocked in Apigee Edge we should not block
  // its Drupal user automatically when syncing.
  if ($original_user
    ->isActive() && $result
    ->getUser()
    ->isBlocked()) {
    $result
      ->getUser()
      ->activate();
  }
  $context = [
    'link' => $result
      ->getUser()
      ->toLink(t('View user'))
      ->toString(),
  ];
  $this
    ->logConversionProblems($result
    ->getProblems(), $context);

  // Rollback a synchronised field's value if the related attribute's value
  // contained an incorrect field value.
  if (count($result
    ->getProblems()) > 0) {

    /** @var \Drupal\user\UserInterface $original_user */
    foreach ($result
      ->getProblems() as $problem) {

      // Do not apply rollback on base fields.
      if ($problem instanceof DeveloperToUserConversationInvalidValueException && !in_array($problem
        ->getTarget(), $this
        ->userDeveloperConverter()::DEVELOPER_PROP_USER_BASE_FIELD_MAP, TRUE)) {
        $result
          ->getUser()
          ->set($problem
          ->getTarget(), $original_user
          ->get($problem
          ->getTarget())
          ->getValue());
      }
    }
  }
}