class DeveloperSync in Apigee Edge 8
A job that synchronizes Apigee Edge developers and Drupal users.
Hierarchy
- class \Drupal\apigee_edge\Job\Job
- class \Drupal\apigee_edge\Job\EdgeJob
- class \Drupal\apigee_edge\Job\DeveloperSync uses JobCreatorTrait
- class \Drupal\apigee_edge\Job\EdgeJob
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\JobView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DeveloperSync:: |
protected | property | All Drupal users indexed by their emails. | |
DeveloperSync:: |
protected | property | All Apigee Edge developers indexed by their emails. | |
DeveloperSync:: |
protected | property | Filter regexp for the Apigee Edge developer emails. | |
DeveloperSync:: |
public | function |
Executes this job. Overrides EdgeJob:: |
|
DeveloperSync:: |
protected | function |
Executes the request itself. Overrides EdgeJob:: |
|
DeveloperSync:: |
protected | function | Loads all Apigee Edge developers indexed my their emails. | |
DeveloperSync:: |
protected | function | Loads all Drupal users indexed my their emails. | |
DeveloperSync:: |
public | function |
DeveloperSync constructor. Overrides Job:: |
|
DeveloperSync:: |
public | function |
Returns this job's textual representation. Overrides Job:: |
|
EdgeJob:: |
protected | property | Request data. | |
EdgeJob:: |
protected | property | Response data. | |
EdgeJob:: |
protected | function | Returns the SDK connector instance from the global container. | |
EdgeJob:: |
public | function |
Returns this job's visual representation. Overrides Job:: |
|
Job:: |
protected | property | Exception storage. | |
Job:: |
private | property | Job ID. | |
Job:: |
protected | property | Messages storage. | |
Job:: |
protected | property | Remaining retries. | |
Job:: |
protected | property | Job status. | |
Job:: |
private | property | The tag of the job. | |
Job:: |
protected | constant | Job statuses. | |
Job:: |
public | function | Consumes a retry. | |
Job:: |
public | constant | Job is failed, and it won't be retried. | |
Job:: |
public | constant | Job is finished successfully. | |
Job:: |
public | function | Gets all stored exception data. | |
Job:: |
public | function | Gets the job id. | |
Job:: |
public | function | Gets all stored messages. | |
Job:: |
public | function | Gets the status of the job. | |
Job:: |
public | function | Gets the job tag. | |
Job:: |
public | constant | Job is waiting to be picked up by a worker. | |
Job:: |
public | function | Adds an exception to the exception storage. | |
Job:: |
public | function | Adds a message to the message storage. | |
Job:: |
public | constant | Job failed, waiting to be retried. | |
Job:: |
public | constant | Job is running. | |
Job:: |
public | constant | Job is claimed by a worker, but not running yet. | |
Job:: |
public | function | Sets the status of the job. | |
Job:: |
public | function | Sets the job tag. | |
Job:: |
public | function | Whether this job should be retried when an exception is thrown. | |
JobCreatorTrait:: |
protected | function | Returns the job executor service. | |
JobCreatorTrait:: |
protected | function | Schedules a job for execution. | |
JobCreatorTrait:: |
protected | function | Schedules multiple jobs for execution. |