You are here

class Manager in Taxonomy Term Depth 8.2

Same name and namespace in other branches
  1. 8 src/QueueManager/Manager.php \Drupal\taxonomy_term_depth\QueueManager\Manager

Hierarchy

  • class \Drupal\taxonomy_term_depth\QueueManager\Manager

Expanded class hierarchy of Manager

1 string reference to 'Manager'
taxonomy_term_depth.services.yml in ./taxonomy_term_depth.services.yml
taxonomy_term_depth.services.yml
1 service uses Manager
taxonomy_term_depth.queue_service in ./taxonomy_term_depth.services.yml
Drupal\taxonomy_term_depth\QueueManager\Manager

File

src/QueueManager/Manager.php, line 11

Namespace

Drupal\taxonomy_term_depth\QueueManager
View source
class Manager {
  const QUEUE_ID = 'taxonomy_term_depth_update_depth';
  const BATCH_COUNT = 20;
  protected $vid = NULL;

  /**
   * @var QueueInterface
   */
  protected $queue = NULL;
  public function setVid($vid = NULL) {
    $this->vid = $vid;
    return $this;
  }
  public function __construct() {
    $this
      ->setVid();

    /** @var QueueFactory $queue_factory */
    $queue_factory = \Drupal::service('queue');

    /** @var QueueInterface $queue */
    $this->queue = $queue_factory
      ->get(static::QUEUE_ID);
  }
  public function clear() {
    $this->queue
      ->deleteQueue();
  }
  public function queueSize() {
    return $this->queue
      ->numberOfItems();
  }
  public function queueBatch($queue_all = TRUE) {
    $query = $this
      ->getTermsQuery();
    if (!$queue_all) {
      $query
        ->isNull('td.depth_level');
    }
    else {

      // Delete queue if have one.
      $this
        ->clear();
    }
    $ids = [];
    foreach ($query
      ->execute() as $row) {
      if (count($ids) >= static::BATCH_COUNT) {
        $this
          ->queueByIds($ids);
        $ids = [];
      }
      $ids[] = $row->tid;
    }

    // Queue remaining items.
    $this
      ->queueByIds($ids);
    return TRUE;
  }
  public function queueBatchMissing() {
    return $this
      ->queueBatch(FALSE);
  }
  public function queueByIds($ids) {
    if (empty($ids)) {
      return FALSE;
    }
    $this
      ->clearDepths($ids);
    foreach ($ids as $tid) {
      $this->queue
        ->createItem([
        'tid' => $tid,
      ]);
    }
    $this
      ->processQueue();
    return TRUE;
  }
  public function clearDepths($ids = NULL) {
    $query = \Drupal::database()
      ->update('taxonomy_term_field_data');
    $query
      ->fields([
      'depth_level' => NULL,
    ]);
    if ($this->vid !== NULL) {
      $query
        ->condition('vid', $this->vid);
    }
    if ($ids !== NULL && is_array($ids) && !empty($ids)) {
      $query
        ->condition('tid', $ids, 'IN');
    }
    return $query
      ->execute();
  }
  protected function getTermsQuery() {
    $query = \Drupal::database()
      ->select('taxonomy_term_field_data', 'td');
    $query
      ->fields('td', [
      'tid',
    ]);
    if ($this->vid !== NULL) {
      $query
        ->condition('td.vid', $this->vid);
    }
    return $query;
  }
  public function processQueue() {
    $queue_worker = \Drupal::service('plugin.manager.queue_worker')
      ->createInstance('taxonomy_term_depth_update_depth');
    while ($item = $this->queue
      ->claimItem()) {
      try {
        $queue_worker
          ->processItem($item->data);
        $this->queue
          ->deleteItem($item);
      } catch (SuspendQueueException $e) {
        $this->queue
          ->releaseItem($item);
        break;
      } catch (\Exception $e) {
        watchdog_exception('npq', $e);
      }
    }
  }
  public function processNextItem() {
    $item = $this->queue
      ->claimItem();
    taxonomy_term_depth_get_by_tid($item['tid'], TRUE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Manager::$queue protected property
Manager::$vid protected property
Manager::BATCH_COUNT constant
Manager::clear public function
Manager::clearDepths public function
Manager::getTermsQuery protected function
Manager::processNextItem public function
Manager::processQueue public function
Manager::queueBatch public function
Manager::queueBatchMissing public function
Manager::queueByIds public function
Manager::queueSize public function
Manager::QUEUE_ID constant
Manager::setVid public function
Manager::__construct public function