You are here

public function DeveloperSync::execute in Apigee Edge 8

Executes this job.

This function should be called only by the JobExecutor.

Return value

bool Whether the job is incomplete. Returning TRUE here means that the job will be rescheduled.

Overrides EdgeJob::execute

File

src/Job/DeveloperSync.php, line 136

Class

DeveloperSync
A job that synchronizes Apigee Edge developers and Drupal users.

Namespace

Drupal\apigee_edge\Job

Code

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;
}