BatchTrait.php in Simple XML sitemap 4.x        
                          
                  
                        
  
  
  
  
File
  src/Queue/BatchTrait.php
  
    View source  
  <?php
namespace Drupal\simple_sitemap\Queue;
use Drupal\Core\StringTranslation\StringTranslationTrait;
trait BatchTrait {
  use StringTranslationTrait;
  
  protected $batch;
  protected static $batchErrorMessage = 'The generation failed to finish. It can be continued manually on the module\'s settings page, or via drush.';
  
  public function batchGenerateSitemap(string $from = self::GENERATE_TYPE_FORM, ?array $variants = NULL) : bool {
    $this->batch = [
      'title' => $this
        ->t('Generating XML sitemaps'),
      'init_message' => $this
        ->t('Initializing...'),
      'error_message' => $this
        ->t(self::$batchErrorMessage),
      'progress_message' => $this
        ->t('Processing items from the queue.<br>Each sitemap variant gets published after all of its items have been processed.'),
      'operations' => [
        [
          __CLASS__ . '::' . 'doBatchGenerateSitemap',
          [],
        ],
      ],
      'finished' => [
        __CLASS__,
        'finishGeneration',
      ],
    ];
    switch ($from) {
      case self::GENERATE_TYPE_FORM:
        
        batch_set($this->batch);
        return TRUE;
      case self::GENERATE_TYPE_DRUSH:
        
        batch_set($this->batch);
        
        $this->batch =& batch_get();
        $this->batch['progressive'] = FALSE;
        drush_backend_batch_process();
        return TRUE;
    }
    return FALSE;
  }
  
  public static function doBatchGenerateSitemap(&$context) : void {
    
    $queue_worker = \Drupal::service('simple_sitemap.queue_worker');
    $queue_worker
      ->generateSitemap();
    $processed_element_count = $queue_worker
      ->getProcessedElementCount();
    $original_element_count = $queue_worker
      ->getInitialElementCount();
    $context['message'] = t('@indexed out of @total total queue items have been processed.', [
      '@indexed' => $processed_element_count,
      '@total' => $original_element_count,
    ]);
    $context['finished'] = $original_element_count > 0 ? $processed_element_count / $original_element_count : 1;
  }
  
  public static function finishGeneration(bool $success, array $results, array $operations) : bool {
    if ($success) {
      \Drupal::service('simple_sitemap.logger')
        ->m('The XML sitemaps have been regenerated.')
        ->log('info');
    }
    else {
      \Drupal::service('simple_sitemap.logger')
        ->m(self::$batchErrorMessage)
        ->display('error', 'administer sitemap settings')
        ->log('error');
    }
    return $success;
  }
}