You are here

class DeveloperSync in Apigee Edge 8

A job that synchronizes Apigee Edge developers and Drupal users.

Hierarchy

Expanded class hierarchy of DeveloperSync

1 file declares its use of DeveloperSync
DeveloperSyncController.php in src/Controller/DeveloperSyncController.php

File

src/Job/DeveloperSync.php, line 28

Namespace

Drupal\apigee_edge\Job
View source
class DeveloperSync extends EdgeJob {
  use JobCreatorTrait;

  /**
   * All Apigee Edge developers indexed by their emails.
   *
   * Format: mb_strtolower(email) => Developer.
   *
   * @var \Drupal\apigee_edge\Entity\DeveloperInterface[]
   *
   * @see https://www.drupal.org/project/drupal/issues/2490294
   */
  protected $edgeDevelopers = [];

  /**
   * All Drupal users indexed by their emails.
   *
   * Format: mb_strtolower(mail) => User.
   *
   * @var \Drupal\user\UserInterface[]
   *
   * @see https://www.drupal.org/project/drupal/issues/2490294
   */
  protected $drupalUsers = [];

  /**
   * Filter regexp for the Apigee Edge developer emails.
   *
   * @var string
   */
  protected $filter = NULL;

  /**
   * DeveloperSync constructor.
   *
   * @param null|string $filter
   *   An optional regexp filter for the Apigee Edge developer emails.
   */
  public function __construct(?string $filter) {
    parent::__construct();
    $this->filter = $filter;
  }

  /**
   * Loads all Drupal users indexed my their emails.
   *
   * @return \Drupal\user\UserInterface[]
   *   Format: mb_strtolower(mail) => User
   *
   * @see https://www.drupal.org/project/drupal/issues/2490294
   */
  protected function loadUsers() : array {
    $users = [];

    /** @var \Drupal\user\UserInterface $user */
    foreach (User::loadMultiple() as $user) {
      $email = $user
        ->getEmail();
      if (isset($email)) {
        if ($this->filter && !preg_match($this->filter, $email)) {
          continue;
        }
        else {
          $users[mb_strtolower($email)] = $user;
        }
      }
    }
    return $users;
  }

  /**
   * Loads all Apigee Edge developers indexed my their emails.
   *
   * @return \Drupal\apigee_edge\Entity\DeveloperInterface[]
   *   Format: mb_strtolower(email) => Developer
   *
   * @see https://www.drupal.org/project/drupal/issues/2490294
   */
  protected function loadDevelopers() : array {

    // Reset developer cache, because the developer may be edited on Apigee
    // Edge.
    \Drupal::entityTypeManager()
      ->getStorage('developer')
      ->resetCache();
    $developers = [];

    /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
    foreach (Developer::loadMultiple() as $developer) {
      $email = $developer
        ->getEmail();
      if ($this->filter && !preg_match($this->filter, $email)) {
        continue;
      }
      else {
        $developers[mb_strtolower($email)] = $developer;
      }
    }
    return $developers;
  }

  /**
   * {@inheritdoc}
   */
  protected function executeRequest() {
    $this->drupalUsers = $this
      ->loadUsers();
    $this->edgeDevelopers = $this
      ->loadDevelopers();
  }

  /**
   * {@inheritdoc}
   */
  public function execute() : bool {
    parent::execute();

    // Update Apigee Edge developers and Drupal users if needed.
    $identical_entities = array_intersect_key($this->edgeDevelopers, $this->drupalUsers);
    foreach ($identical_entities as $clean_email => $entity) {

      /** @var \Drupal\apigee_edge\Entity\DeveloperInterface $developer */
      $developer = $this->edgeDevelopers[$clean_email];

      /** @var \Drupal\user\UserInterface $user */
      $user = $this->drupalUsers[$clean_email];
      $last_modified_delta = $developer
        ->getLastModifiedAt()
        ->getTimestamp() - $user
        ->getChangedTime();

      // Update Drupal user because the Apigee Edge developer is the most
      // recent.
      if ($last_modified_delta >= 0) {
        $update_user_job = new UserUpdate($user
          ->getEmail());
        $update_user_job
          ->setTag($this
          ->getTag());
        $this
          ->scheduleJob($update_user_job);
      }
      elseif ($last_modified_delta < 0) {
        $update_developer_job = new DeveloperUpdate($developer
          ->getEmail());
        $update_developer_job
          ->setTag($this
          ->getTag());
        $this
          ->scheduleJob($update_developer_job);
      }
    }

    // Create missing Drupal users.
    foreach ($this->edgeDevelopers as $clean_email => $developer) {
      if (empty($this->drupalUsers[$clean_email])) {
        $create_user_job = new UserCreate($developer
          ->getEmail());
        $create_user_job
          ->setTag($this
          ->getTag());
        $this
          ->scheduleJob($create_user_job);
      }
    }

    // Create missing Apigee Edge developers.
    foreach ($this->drupalUsers as $clean_email => $user) {
      if (empty($this->edgeDevelopers[$clean_email])) {
        $create_developer_job = new DeveloperCreate($user
          ->getEmail());
        $create_developer_job
          ->setTag($this
          ->getTag());
        $this
          ->scheduleJob($create_developer_job);
      }
    }

    // Reset these, so they won't be saved to the database, taking up space.
    $this->edgeDevelopers = [];
    $this->drupalUsers = [];
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() : string {
    return t('Synchronizing Apigee Edge developers and Drupal users.')
      ->render();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeveloperSync::$drupalUsers protected property All Drupal users indexed by their emails.
DeveloperSync::$edgeDevelopers protected property All Apigee Edge developers indexed by their emails.
DeveloperSync::$filter protected property Filter regexp for the Apigee Edge developer emails.
DeveloperSync::execute public function Executes this job. Overrides EdgeJob::execute
DeveloperSync::executeRequest protected function Executes the request itself. Overrides EdgeJob::executeRequest
DeveloperSync::loadDevelopers protected function Loads all Apigee Edge developers indexed my their emails.
DeveloperSync::loadUsers protected function Loads all Drupal users indexed my their emails.
DeveloperSync::__construct public function DeveloperSync constructor. Overrides Job::__construct
DeveloperSync::__toString public function Returns this job's textual representation. Overrides Job::__toString
EdgeJob::$request protected property Request data.
EdgeJob::$response protected property Response data.
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.
JobCreatorTrait::getExecutor protected function Returns the job executor service.
JobCreatorTrait::scheduleJob protected function Schedules a job for execution.
JobCreatorTrait::scheduleJobs protected function Schedules multiple jobs for execution.