You are here

class GenerateBatch in File Hash 8

Generates file hashes in bulk.

Hierarchy

Expanded class hierarchy of GenerateBatch

3 files declare their use of GenerateBatch
filehash.drush.inc in ./filehash.drush.inc
Drush integration for File Hash module.
FileHashCommands.php in src/Commands/FileHashCommands.php
FileHashGenerateForm.php in src/Form/FileHashGenerateForm.php

File

src/Batch/GenerateBatch.php, line 10

Namespace

Drupal\filehash\Batch
View source
class GenerateBatch {

  /**
   * Creates the batch definition.
   *
   * @return array
   *   The batch definition.
   */
  public static function createBatch() {
    return [
      'operations' => [
        [
          '\\Drupal\\filehash\\Batch\\GenerateBatch::process',
          [],
        ],
      ],
      'finished' => '\\Drupal\\filehash\\Batch\\GenerateBatch::finished',
      'title' => t('Processing file hash batch'),
      'init_message' => t('File hash batch is starting.'),
      'progress_message' => t('Please wait...'),
      'error_message' => t('File hash batch has encountered an error.'),
    ];
  }

  /**
   * Returns count of files in file_managed table.
   *
   * @return int
   *   The count of managed files.
   */
  public static function count() {
    return \Drupal::database()
      ->query('SELECT COUNT(*) FROM {file_managed}')
      ->fetchField();
  }

  /**
   * Batch process callback.
   */
  public static function process(&$context) {
    if (!isset($context['results']['processed'])) {
      $context['results']['processed'] = 0;
      $context['results']['updated'] = 0;
      $context['sandbox']['count'] = self::count();
    }
    $files = \Drupal::database()
      ->select('file_managed')
      ->fields('file_managed', [
      'fid',
    ])
      ->range($context['results']['processed'], 1)
      ->execute();
    foreach ($files as $file) {

      // Fully load file object.
      $file = File::load($file->fid);
      $variables = [
        '%url' => $file
          ->getFileUri(),
      ];
      $context['message'] = t('Generated file hash for %url.', $variables);
    }
    $context['results']['processed']++;
    $context['finished'] = $context['sandbox']['count'] ? $context['results']['processed'] / $context['sandbox']['count'] : 1;
  }

  /**
   * Batch finish callback.
   */
  public static function finished($success, $results, $operations) {
    $variables = [
      '@processed' => $results['processed'],
    ];
    if ($success) {
      \Drupal::messenger()
        ->addMessage(t('Processed @processed files.', $variables));
    }
    else {
      \Drupal::messenger()
        ->addWarning(t('An error occurred after processing @processed files.', $variables));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GenerateBatch::count public static function Returns count of files in file_managed table.
GenerateBatch::createBatch public static function Creates the batch definition.
GenerateBatch::finished public static function Batch finish callback.
GenerateBatch::process public static function Batch process callback.