View source
<?php
namespace Drupal\commerce_license;
use Drupal\advancedqueue\Job;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class Cron implements CronInterface {
protected $entityTypeManager;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->time = $time;
}
public function run() {
$time = $this->time
->getRequestTime();
$license_ids = $this
->getLicensesToExpire($time);
if ($license_ids) {
$queue_storage = $this->entityTypeManager
->getStorage('advancedqueue_queue');
$queue = $queue_storage
->load('commerce_license');
foreach ($license_ids as $license_id) {
$expire_license_job = Job::create('commerce_license_expire', [
'license_id' => $license_id,
]);
$queue
->enqueueJob($expire_license_job);
}
}
}
protected function getLicensesToExpire($time) {
$query = $this->entityTypeManager
->getStorage('commerce_license')
->getQuery()
->condition('state', 'active')
->condition('expires', $time, '<=')
->condition('expires', 0, '<>');
$license_ids = $query
->execute();
return $license_ids;
}
}