You are here

class Cron in Commerce License 8.2

Default cron implementation.

Hierarchy

Expanded class hierarchy of Cron

1 string reference to 'Cron'
commerce_license.services.yml in ./commerce_license.services.yml
commerce_license.services.yml
1 service uses Cron
commerce_license.cron in ./commerce_license.services.yml
\Drupal\commerce_license\Cron

File

src/Cron.php, line 12

Namespace

Drupal\commerce_license
View 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

Namesort descending Modifiers Type Description Overrides
Cron::$entityTypeManager protected property The entity type manager.
Cron::$time protected property The time.
Cron::getLicensesToExpire protected function Gets IDs of licenses that are set to expire.
Cron::run public function Runs the cron. Overrides CronInterface::run
Cron::__construct public function Constructs a new Cron object.