class LinkCheckerBatch in Link checker 8
Helper service to handle links checking.
Hierarchy
- class \Drupal\linkchecker\LinkCheckerBatch uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
 
Expanded class hierarchy of LinkCheckerBatch
1 file declares its use of LinkCheckerBatch
- LinkCheckerCommands.php in src/
Commands/ LinkCheckerCommands.php  
1 string reference to 'LinkCheckerBatch'
1 service uses LinkCheckerBatch
File
- src/
LinkCheckerBatch.php, line 18  
Namespace
Drupal\linkcheckerView source
class LinkCheckerBatch {
  use DependencySerializationTrait;
  use MessengerTrait;
  use StringTranslationTrait;
  /**
   * The link checker.
   *
   * @var \Drupal\linkchecker\LinkCheckerService
   */
  protected $checker;
  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  /**
   * The lock.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;
  /**
   * The queue factory.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;
  /**
   * The queue worker manager.
   *
   * @var \Drupal\Core\Queue\QueueWorkerManagerInterface
   */
  protected $queueWorkerManager;
  /**
   * LinkExtractorBatch constructor.
   */
  public function __construct(LinkCheckerService $checker, EntityTypeManagerInterface $entityTypeManager, LockBackendInterface $lock, QueueFactory $queueFactory, QueueWorkerManagerInterface $queueWorkerManager) {
    $this->checker = $checker;
    $this->entityTypeManager = $entityTypeManager;
    $this->lock = $lock;
    $this->queueFactory = $queueFactory;
    $this->queueWorkerManager = $queueWorkerManager;
  }
  /**
   * Process next item in queue.
   */
  public function processQueueItem() {
    $item = $this->queueFactory
      ->get('linkchecker_check')
      ->claimItem();
    if (!empty($item)) {
      $this->queueWorkerManager
        ->createInstance('linkchecker_check')
        ->processItem($item->data);
    }
  }
  /**
   * Sets a batch to extract links from entities.
   */
  public function batch() {
    // Get max_execution_time from configuration, override 0 with 240 seconds.
    $maxExecutionTime = ini_get('max_execution_time') == 0 ? 240 : ini_get('max_execution_time');
    // Make sure we have enough time to validate all of the links.
    Environment::setTimeLimit($maxExecutionTime);
    // Make sure this is the only process trying to run this function.
    if (!$this->lock
      ->acquire('linkchecker_check', $maxExecutionTime) && FALSE) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('Attempted to re-run link checks while they are already running.'));
      return;
    }
    $batch = new BatchBuilder();
    $batch
      ->setTitle('Check links')
      ->addOperation([
      $this,
      'batchProcessQueue',
    ])
      ->setProgressive()
      ->setFinishCallback([
      $this,
      'batchFinished',
    ]);
    batch_set($batch
      ->toArray());
  }
  /**
   * Gets total number of links to process.
   *
   * @return int
   *   Total number of links.
   */
  public function getTotalLinksToProcess() {
    return $this->checker
      ->queueLinks(TRUE);
  }
  /**
   * Process linkchecker_check queue.
   *
   * @param mixed $context
   *   Context data from batch API.
   */
  public function batchProcessQueue(&$context) {
    if (!isset($context['sandbox']['total'])) {
      $context['sandbox']['total'] = $this
        ->getTotalLinksToProcess();
      $context['sandbox']['current'] = 0;
    }
    $this
      ->processQueueItem();
    $context['sandbox']['current']++;
    if (!empty($context['sandbox']['total'])) {
      $context['finished'] = $context['sandbox']['current'] / $context['sandbox']['total'];
    }
    else {
      $context['finished'] = 1;
    }
  }
  /**
   * Finished callback for batch.
   */
  public function batchFinished($success) {
    if ($success) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Links were successfully checked.'));
    }
    else {
      $this
        ->messenger()
        ->addError($this
        ->t('Links were not checked.'));
    }
  }
}Members
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of service IDs keyed by property name used for serialization. | |
| 
            DependencySerializationTrait:: | 
                  public | function | 1 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            LinkCheckerBatch:: | 
                  protected | property | The link checker. | |
| 
            LinkCheckerBatch:: | 
                  protected | property | The entity type manager. | |
| 
            LinkCheckerBatch:: | 
                  protected | property | The lock. | |
| 
            LinkCheckerBatch:: | 
                  protected | property | The queue factory. | |
| 
            LinkCheckerBatch:: | 
                  protected | property | The queue worker manager. | |
| 
            LinkCheckerBatch:: | 
                  public | function | Sets a batch to extract links from entities. | |
| 
            LinkCheckerBatch:: | 
                  public | function | Finished callback for batch. | |
| 
            LinkCheckerBatch:: | 
                  public | function | Process linkchecker_check queue. | |
| 
            LinkCheckerBatch:: | 
                  public | function | Gets total number of links to process. | |
| 
            LinkCheckerBatch:: | 
                  public | function | Process next item in queue. | |
| 
            LinkCheckerBatch:: | 
                  public | function | LinkExtractorBatch constructor. | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 1 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. |