You are here

abstract class DeveloperCreateUpdate in Apigee Edge 8

Base class for user create/update sync jobs.

Hierarchy

Expanded class hierarchy of DeveloperCreateUpdate

File

src/Job/DeveloperCreateUpdate.php, line 34

Namespace

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

  /**
   * The Drupal user's email.
   *
   * @var string
   */
  protected $email;

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

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

      /** @var \Drupal\user\UserInterface $account */
      $account = user_load_by_mail($this->email);
      if (!$account) {
        throw new UserDoesNotExistWithEmail($this->email);
      }
      $result = $this
        ->userDeveloperConverter()
        ->convertUser($account);
      $this
        ->beforeDeveloperSave($result, $account);

      // Do not save user if there were no changes.
      if ($result
        ->getSuccessfullyAppliedChanges() > 0) {
        $result
          ->getDeveloper()
          ->save();
      }
    } catch (\Exception $exception) {
      $message = '@operation: Skipping %mail developer. @message %function (line %line of %file). <pre>@backtrace_string</pre>';
      $context = [
        '%mail' => $this->email,
        'link' => $account
          ->toLink(t('View user'))
          ->toString(),
        '@operation' => get_class($this),
      ];
      $context += Error::decodeException($exception);
      $this
        ->logger()
        ->error($message, $context);
      $this
        ->recordMessage(t('Skipping %mail developer: @message', $context)
        ->render());
    } finally {
      if (isset($result)) {
        $this
          ->afterDeveloperSave($result, $account);
      }
    }
  }

  /**
   * Execute actions before the developer gets saved.
   *
   * @param \Drupal\apigee_edge\Structure\UserToDeveloperConversionResult $result
   *   The result of the entity conversion.
   * @param \Drupal\user\UserInterface $user
   *   The converted user entity.
   *
   * @throws \Exception
   *   Can throw exception to abort developer save.
   */
  protected function beforeDeveloperSave(UserToDeveloperConversionResult $result, UserInterface $user) : void {
    $context = [
      'link' => $user
        ->toLink(t('View user'))
        ->toString(),
    ];
    $this
      ->logConversionProblems($result
      ->getProblems(), $context);
  }

  /**
   * Execute actions after the developer has been saved.
   *
   * Actions here always gets executed even if the developer save has failed.
   *
   * @param \Drupal\apigee_edge\Structure\UserToDeveloperConversionResult $result
   *   The result of the entity conversion.
   * @param \Drupal\user\UserInterface $user
   *   The converted user entity.
   */
  protected function afterDeveloperSave(UserToDeveloperConversionResult $result, UserInterface $user) : void {
  }

  /**
   * {@inheritdoc}
   */
  protected function logConversionProblem(UserDeveloperConversionException $problem, array $context = []) : void {
    $ro = new \ReflectionObject($this);
    $context = [
      '%mail' => $this->email,
      '@operation' => $ro
        ->getShortName(),
    ];
    if ($problem instanceof UserDeveloperConversionUserFieldDoesNotExistException) {
      $message = "@operation: %mail developer's %attribute_name attribute has been skipped because %field_name field does not exist.";
      $context['%field_name'] = $problem
        ->getFieldName();
      $context['%attribute_name'] = $this
        ->fieldAttributeConverter()
        ->getAttributeName($problem
        ->getFieldName());
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t("%mail developer's %attribute_name attribute has been skipped because %field_name field does not exist.", $context)
        ->render());
    }
    elseif ($problem instanceof UserDeveloperConversionNoStorageFormatterFoundException) {
      $message = "@operation: %mail developer's %attribute_name attribute has been skipped because there is no available storage formatter for %field_type field type.";
      $context['%field_type'] = $problem
        ->getFieldDefinition()
        ->getType();
      $context['%attribute_name'] = $this
        ->fieldAttributeConverter()
        ->getAttributeName($problem
        ->getFieldDefinition()
        ->getName());
      $this
        ->logger()
        ->warning($message, $context);
      $this
        ->recordMessage(t("%mail developer's %attribute_name attribute has been skipped because there is no available storage formatter for %field_type field type.", $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 developer: @message", $context)
        ->render());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeveloperCreateUpdate::$email protected property The Drupal user's email.
DeveloperCreateUpdate::afterDeveloperSave protected function Execute actions after the developer has been saved.
DeveloperCreateUpdate::beforeDeveloperSave protected function Execute actions before the developer gets saved.
DeveloperCreateUpdate::executeRequest protected function Executes the request itself. Overrides EdgeJob::executeRequest
DeveloperCreateUpdate::logConversionProblem protected function Logs an entity conversation problems encountered meanwhile syncing. Overrides UserDeveloperSyncJobTrait::logConversionProblem
DeveloperCreateUpdate::__construct public function DeveloperCreateUpdate constructor. Overrides Job::__construct
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
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.