class Cron in Commerce License 8.2
Default cron implementation.
Hierarchy
- class \Drupal\commerce_license\Cron implements CronInterface
Expanded class hierarchy of Cron
1 string reference to 'Cron'
1 service uses Cron
File
- src/
Cron.php, line 12
Namespace
Drupal\commerce_licenseView source
class Cron implements CronInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The time.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* Constructs a new Cron object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->time = $time;
}
/**
* {@inheritdoc}
*/
public function run() {
$time = $this->time
->getRequestTime();
$license_ids = $this
->getLicensesToExpire($time);
if ($license_ids) {
$queue_storage = $this->entityTypeManager
->getStorage('advancedqueue_queue');
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
$queue = $queue_storage
->load('commerce_license');
foreach ($license_ids as $license_id) {
// Create a job and queue each one up.
$expire_license_job = Job::create('commerce_license_expire', [
'license_id' => $license_id,
]);
$queue
->enqueueJob($expire_license_job);
}
}
}
/**
* Gets IDs of licenses that are set to expire.
*
* @param int $time
* Time to check against license expiration.
*
* @return array
* IDs of matching commerce_license entities.
*/
protected function getLicensesToExpire($time) {
// Get all of the active expired licenses.
$query = $this->entityTypeManager
->getStorage('commerce_license')
->getQuery()
->condition('state', 'active')
->condition('expires', $time, '<=')
->condition('expires', 0, '<>');
$license_ids = $query
->execute();
return $license_ids;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Cron:: |
protected | property | The entity type manager. | |
Cron:: |
protected | property | The time. | |
Cron:: |
protected | function | Gets IDs of licenses that are set to expire. | |
Cron:: |
public | function |
Runs the cron. Overrides CronInterface:: |
|
Cron:: |
public | function | Constructs a new Cron object. |