You are here

public function WebformScheduledEmailManager::cron in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_scheduled_email/src/WebformScheduledEmailManager.php \Drupal\webform_scheduled_email\WebformScheduledEmailManager::cron()

Cron task for scheduling and sending emails.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: A webform or webform submission.

string|null $handler_id: A webform handler id.

int $schedule_limit: The maximum number of emails to be scheduled. If set to 0 no emails will be scheduled.

int $send_limit: The maximum number of emails to be sent. If set to 0 no emails will be sent. Defaults to webform.settting->batch.default_batch_email_size.

Return value

array An associative array containing cron task stats. Includes:

Overrides WebformScheduledEmailManagerInterface::cron

File

modules/webform_scheduled_email/src/WebformScheduledEmailManager.php, line 466

Class

WebformScheduledEmailManager
Defines the webform scheduled email manager.

Namespace

Drupal\webform_scheduled_email

Code

public function cron(EntityInterface $entity = NULL, $handler_id = NULL, $schedule_limit = 1000, $send_limit = NULL) {

  // Get default batch email size.
  if ($send_limit === NULL) {
    $send_limit = $this->configFactory
      ->get('webform.settings')
      ->get('batch.default_batch_email_size') ?: 500;
  }
  $stats = [];
  $stats += $this
    ->cronSchedule($entity, $handler_id, $schedule_limit);
  $stats += $this
    ->cronSend($entity, $handler_id, $send_limit);

  // Build summary.
  $labels = [
    WebformScheduledEmailManagerInterface::EMAIL_SCHEDULED => $this
      ->t('scheduled'),
    WebformScheduledEmailManagerInterface::EMAIL_RESCHEDULED => $this
      ->t('rescheduled'),
    WebformScheduledEmailManagerInterface::EMAIL_ALREADY_SCHEDULED => $this
      ->t('already scheduled'),
    WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED => $this
      ->t('unscheduled'),
    WebformScheduledEmailManagerInterface::EMAIL_IGNORED => $this
      ->t('ignored'),
    WebformScheduledEmailManagerInterface::EMAIL_SENT => $this
      ->t('sent'),
    WebformScheduledEmailManagerInterface::EMAIL_NOT_SENT => $this
      ->t('not sent'),
    WebformScheduledEmailManagerInterface::EMAIL_SKIPPED => $this
      ->t('skipped'),
  ];
  $summary = [];
  foreach ($stats as $type => $total) {
    $summary[] = $labels[$type] . ' = ' . $total;
  }
  $stats['_summary'] = implode('; ', $summary);

  // Build message with context.
  $context = [
    '@summary' => $stats['_summary'],
  ];
  $message = 'Cron task executed. (@summary)';
  if ($entity) {
    $context['@entity'] = $entity
      ->label();
    $message = '@entity: Cron task executed. (@summary)';
    if ($entity instanceof WebformInterface && $handler_id) {
      $context['@handler'] = $entity
        ->getHandler($handler_id)
        ->label();
      $message = "@entity: Cron task executed '@handler' handler. (@summary)";
    }
  }
  $this
    ->getLogger()
    ->notice($message, $context);
  $stats['_message'] = $message;
  $stats['_context'] = $context;
  return $stats;
}