You are here

public static function CouponGenerateForm::finishBatch in Commerce Core 8.2

Batch finished callback: display batch statistics.

Parameters

bool $success: Indicates whether the batch has completed successfully.

mixed[] $results: The array of results gathered by the batch processing.

string[] $operations: If $success is FALSE, contains the operations that remained unprocessed.

File

modules/promotion/src/Form/CouponGenerateForm.php, line 281

Class

CouponGenerateForm
Provides a form for bulk generating coupons.

Namespace

Drupal\commerce_promotion\Form

Code

public static function finishBatch($success, array $results, array $operations) {
  if ($success) {
    $created = count($results['codes']);

    // An incomplete set of coupons was generated.
    if ($created != $results['total_quantity']) {
      \Drupal::messenger()
        ->addWarning(t('Generated %created out of %total requested coupons. Consider adding a unique prefix/suffix or increasing the pattern length to improve results.', [
        '%created' => $created,
        '%total' => $results['total_quantity'],
      ]));
    }
    else {
      \Drupal::messenger()
        ->addMessage(\Drupal::translation()
        ->formatPlural($created, 'Generated 1 coupon.', 'Generated @count coupons.'));
    }
  }
  else {
    $error_operation = reset($operations);
    \Drupal::messenger()
      ->addError(t('An error occurred while processing @operation with arguments: @args', [
      '@operation' => $error_operation[0],
      '@args' => print_r($error_operation[0], TRUE),
    ]));
  }
}