You are here

abstract class UserCreateUpdate in Apigee Edge 8

Base class for user create/update sync jobs.

Hierarchy

Expanded class hierarchy of UserCreateUpdate

File

src/Job/UserCreateUpdate.php, line 37

Namespace

Drupal\apigee_edge\Job
View source
abstract class UserCreateUpdate extends EdgeJob {
  use UserDeveloperSyncJobTrait;

  /**
   * The Apigee Edge developer's email.
   *
   * @var string
   */
  protected $email;

  /**
   * UserCreateUpdate constructor.
   *
   * @param string $email
   *   The email address of the user.
   */
  public function __construct(string $email) {
    parent::__construct();
    $this->email = $email;
  }

  /**
   * {@inheritdoc}
   */
  protected function executeRequest() {
    try {

      /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
      $developer = Developer::load($this->email);
      if ($developer === NULL) {
        throw new DeveloperDoesNotExistException($this->email);
      }
      $result = $this
        ->userDeveloperConverter()
        ->convertDeveloper($developer);
      $this
        ->beforeUserSave($result);

      // Do not save user if there were no changes.
      if ($result
        ->getSuccessfullyAppliedChanges() > 0) {

        // If the developer-user synchronization is in progress, then saving
        // the same developer in apigee_edge_user_presave() while creating
        // Drupal user based on a developer should be avoided.
        _apigee_edge_set_sync_in_progress(TRUE);
        $result
          ->getUser()
          ->save();
      }
    } catch (\Exception $exception) {
      $message = '@operation: Skipping %mail user. @message %function (line %line of %file). <pre>@backtrace_string</pre>';
      $context = [
        '%mail' => $this->email,
        '@operation' => get_class($this),
      ];
      $context += Error::decodeException($exception);
      $this
        ->logger()
        ->error($message, $context);
      $this
        ->recordMessage(t('Skipping %mail user: @message', $context)
        ->render());
    } finally {
      _apigee_edge_set_sync_in_progress(FALSE);
      if (isset($result)) {
        $this
          ->afterUserSave($result);
      }
    }
  }

  /**
   * Execute actions before the user gets saved.
   *
   * @param \Drupal\apigee_edge\Structure\DeveloperToUserConversionResult $result
   *   Result of the entity conversion.
   *
   * @throws \Exception
   *   Can throw an exception to abort user save.
   */
  protected function beforeUserSave(DeveloperToUserConversionResult $result) : void {

    // Abort the operation if any of these special problems occurred
    // meanwhile the conversation.
    foreach ($result
      ->getProblems() as $problem) {

      // Skip user save if username is already taken or the username
      // is too long instead of getting a database exception in a lower layer.
      // (Username field's value is not limited on Apigee Edge and it is not
      // unique either.)
      if ($problem instanceof DeveloperToUserConversationInvalidValueException && $problem
        ->getTarget() === 'name') {
        throw $problem;
      }
    }

    // It's necessary because changed time is automatically updated on the
    // UI only.
    $result
      ->getUser()
      ->setChangedTime(\Drupal::time()
      ->getCurrentTime());
  }

  /**
   * Execute actions after the user has been saved.
   *
   * Actions here always gets executed even if the user save has failed.
   *
   * @param \Drupal\apigee_edge\Structure\DeveloperToUserConversionResult $result
   *   Result of the entity conversion.
   */
  protected function afterUserSave(DeveloperToUserConversionResult $result) : void {
  }

  /**
   * {@inheritdoc}
   */
  protected function logConversionProblem(UserDeveloperConversionException $problem, array $context = []) : void {
    $ro = new \ReflectionObject($this);
    $context += [
      '%mail' => $this->email,
      '@operation' => $ro
        ->getShortName(),
    ];
    if ($problem instanceof DeveloperToUserConversionAttributeDoesNotExistException) {
      $message = '@operation: %field_name field value on %mail user has not changed because the relevant developer does not have %attribute_name attribute on Apigee Edge.';
      $context += [
        '%field_name' => $this
          ->fieldAttributeConverter()
          ->getFieldName($problem
          ->getAttributeName()),
        '%attribute_name' => $problem
          ->getAttributeName(),
      ];
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t("%field_name field value on %mail user has not changed because the relevant developer does not have %attribute_name attribute on Apigee Edge.", $context)
        ->render());
    }
    elseif ($problem instanceof UserDeveloperConversionUserFieldDoesNotExistException) {
      $message = '@operation: %attribute_name attribute has been skipped because %field_name field does not exist on user.';
      $context += [
        '%field_name' => $problem
          ->getFieldName(),
        '%attribute_name' => $this
          ->fieldAttributeConverter()
          ->getAttributeName($problem
          ->getFieldName()),
      ];
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t("%attribute_name attribute has been skipped because %field_name field does not exist on user.", $context)
        ->render());
    }
    elseif ($problem instanceof UserDeveloperConversionNoStorageFormatterFoundException) {
      $message = '@operation: %field_name field has been skipped because there is no available storage formatter for %field_type field type.';
      $context += [
        '%field_name' => $problem
          ->getFieldDefinition()
          ->getName(),
        '%field_type' => $problem
          ->getFieldDefinition()
          ->getType(),
      ];
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t('%field_name field has been skipped because there is no available storage formatter for %field_type field type.', $context)
        ->render());
    }
    elseif ($problem instanceof DeveloperToUserConversationInvalidValueException) {
      $message = "@operation: %field_name field value on %mail user has not changed because %attribute_name attribute's value is invalid as a field value: %message";
      $context += [
        '%field_name' => $problem
          ->getTarget(),
        '%attribute_name' => $problem
          ->getSource(),
        '%field_value' => is_object($problem
          ->getViolation()
          ->getInvalidValue()) ? $problem
          ->getViolation()
          ->getInvalidValue() instanceof ItemList ? var_export($problem
          ->getViolation()
          ->getInvalidValue()
          ->getValue(), TRUE) : $problem
          ->getViolation()
          ->getInvalidValue()->value : $problem
          ->getViolation()
          ->getInvalidValue(),
        '%message' => $problem
          ->getViolation()
          ->getMessage(),
      ];
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t("%field_name field value on %mail user has not changed because %attribute_name attribute's value is invalid as a field value: %message", $context)
        ->render());
    }
    else {
      $context += Error::decodeException($problem);
      $this
        ->logger()
        ->warning('@operation: Unexpected problem occurred while creating %mail user: @message %function (line %line of %file). <pre>@backtrace_string</pre>');
      $this
        ->recordMessage(t("Unexpected problem occurred while processing %mail user: @message", $context)
        ->render());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EdgeJob::$request protected property Request data.
EdgeJob::$response protected property Response data.
EdgeJob::execute public function Executes this job. Overrides Job::execute 1
EdgeJob::getConnector protected function Returns the SDK connector instance from the global container.
EdgeJob::renderArray public function Returns this job's visual representation. Overrides Job::renderArray
Job::$exceptions protected property Exception storage.
Job::$id private property Job ID.
Job::$messages protected property Messages storage.
Job::$retry protected property Remaining retries.
Job::$status protected property Job status.
Job::$tag private property The tag of the job.
Job::ALL_STATUSES protected constant Job statuses.
Job::consumeRetry public function Consumes a retry.
Job::FAILED public constant Job is failed, and it won't be retried.
Job::FINISHED public constant Job is finished successfully.
Job::getExceptions public function Gets all stored exception data.
Job::getId public function Gets the job id.
Job::getMessages public function Gets all stored messages.
Job::getStatus public function Gets the status of the job.
Job::getTag public function Gets the job tag.
Job::IDLE public constant Job is waiting to be picked up by a worker.
Job::recordException public function Adds an exception to the exception storage.
Job::recordMessage public function Adds a message to the message storage.
Job::RESCHEDULED public constant Job failed, waiting to be retried.
Job::RUNNING public constant Job is running.
Job::SELECTED public constant Job is claimed by a worker, but not running yet.
Job::setStatus public function Sets the status of the job.
Job::setTag public function Sets the job tag.
Job::shouldRetry public function Whether this job should be retried when an exception is thrown.
Job::__toString abstract public function Returns this job's textual representation. 5
UserCreateUpdate::$email protected property The Apigee Edge developer's email.
UserCreateUpdate::afterUserSave protected function Execute actions after the user has been saved. 1
UserCreateUpdate::beforeUserSave protected function Execute actions before the user gets saved. 1
UserCreateUpdate::executeRequest protected function Executes the request itself. Overrides EdgeJob::executeRequest
UserCreateUpdate::logConversionProblem protected function Logs an entity conversation problems encountered meanwhile syncing. Overrides UserDeveloperSyncJobTrait::logConversionProblem
UserCreateUpdate::__construct public function UserCreateUpdate constructor. Overrides Job::__construct
UserDeveloperSyncJobTrait::fieldAttributeConverter protected function Field-attribute converter service.
UserDeveloperSyncJobTrait::logConversionProblems protected function Logs all entity conversion problems encountered meanwhile syncing.
UserDeveloperSyncJobTrait::logger protected function Logger interface.
UserDeveloperSyncJobTrait::userDeveloperConverter protected function User-developer converter service.