You are here

class BatchService in Image Style Warmer 8

Class BatchService

@package Drupal\image_style_warmer

Hierarchy

Expanded class hierarchy of BatchService

File

src/BatchService.php, line 12

Namespace

Drupal\image_style_warmer
View source
class BatchService {

  /**
   * Batch process callback.
   *
   * @param int $fid
   *   Id of the file.
   * @param object $context
   *   Context for operations.
   */
  public function warmUpFileProcess($fid, $count, &$context) {

    /** @var \Drupal\image_style_warmer\ImageStylesWarmerInterface $image_styles_warmer */
    $image_styles_warmer = \Drupal::service('image_style_warmer.warmer');
    $file = File::load($fid);
    $image_styles_warmer
      ->warmUp($file);

    // Store some results for post-processing in the 'finished' callback.
    // The contents of 'results' will be available as $results in the
    // 'finished' function (in this example, batch_example_finished()).
    $context['results'][] = $fid;
    $i = count($context['results']);

    // Optional message displayed under the progressbar.
    $context['message'] = t('Warming up styles for file @fid (@i/@count)', [
      '@fid' => $fid,
      '@i' => $i,
      '@count' => $count,
    ]);
  }

  /**
   * Batch Finished callback.
   *
   * @param bool $success
   *   Success of the operation.
   * @param array $results
   *   Array of results for post processing.
   * @param array $operations
   *   Array of operations.
   */
  public function warmUpFileFinished($success, array $results, array $operations) {
    $messenger = \Drupal::messenger();
    if ($success) {
      $messenger
        ->addMessage(t('@count files warmed up.', [
        '@count' => count($results),
      ]));
    }
    else {

      // An error occurred.
      // $operations contains the operations that remained unprocessed.
      $error_operation = reset($operations);
      $messenger
        ->addMessage(t('An error occurred while processing @operation with arguments : @args', [
        '@operation' => $error_operation[0],
        '@args' => print_r($error_operation[0], TRUE),
      ]));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchService::warmUpFileFinished public function Batch Finished callback.
BatchService::warmUpFileProcess public function Batch process callback.