You are here

protected function WebformScheduledEmailManager::cronSchedule 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::cronSchedule()

Schedule emails.

Parameters

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

string|null $handler_id: A webform handler id.

int $limit: The maximum number of schedule emails to be scheduled per request.

Return value

array An associative array containing stats.

1 call to WebformScheduledEmailManager::cronSchedule()
WebformScheduledEmailManager::cron in modules/webform_scheduled_email/src/WebformScheduledEmailManager.php
Cron task for scheduling and sending emails.

File

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

Class

WebformScheduledEmailManager
Defines the webform scheduled email manager.

Namespace

Drupal\webform_scheduled_email

Code

protected function cronSchedule(EntityInterface $entity = NULL, $handler_id = NULL, $limit = 1000) {
  $stats = [
    WebformScheduledEmailManagerInterface::EMAIL_SCHEDULED => 0,
    WebformScheduledEmailManagerInterface::EMAIL_RESCHEDULED => 0,
    WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED => 0,
    WebformScheduledEmailManagerInterface::EMAIL_ALREADY_SCHEDULED => 0,
    WebformScheduledEmailManagerInterface::EMAIL_IGNORED => 0,
  ];
  if (empty($limit)) {
    return $stats;
  }
  list($webform, $webform_submission, $source_entity) = $this
    ->getEntities($entity);
  $query = $this->database
    ->select('webform_scheduled_email', 'w')
    ->fields('w', [
    'eid',
    'sid',
    'webform_id',
    'entity_type',
    'entity_id',
    'handler_id',
    'state',
    'send',
  ])
    ->condition('w.state', [
    WebformScheduledEmailManagerInterface::SUBMISSION_SCHEDULE,
    WebformScheduledEmailManagerInterface::SUBMISSION_UNSCHEDULE,
    WebformScheduledEmailManagerInterface::SUBMISSION_RESCHEDULE,
  ], 'IN')
    ->orderBy('w.send')
    ->orderBy('w.sid')
    ->range(0, $limit);
  $this
    ->addQueryConditions($query, $webform, $webform_submission, $source_entity, $handler_id);

  // Reset $webform, $webform_submission, and $handler_id so that they
  // can be safely used below.
  $webform = NULL;
  $webform_submission = NULL;
  $handler_id = NULL;
  $result = $query
    ->execute();

  // Collect record, webform ids, and submission ids.
  $webform_ids = [];
  $sids = [];
  $records = [];
  foreach ($result as $record) {
    $webform_ids[$record->webform_id] = $record->webform_id;
    $sids[$record->sid] = $record->sid;
    $records[$record->eid] = $record;
  }

  // Bulk load webforms and submission to improve performance.
  if ($webform_ids) {
    $this
      ->getWebformStorage()
      ->loadMultiple($webform_ids);
  }
  $webform_submissions = $sids ? $this
    ->getSubmissionStorage()
    ->loadMultiple($sids) : [];

  // Now update all the emails.
  foreach ($records as $record) {

    // This should never happen but we will delete this record since
    // it is pointing to missing submission.
    if (!isset($webform_submissions[$record->sid])) {
      $this->database
        ->delete('webform_scheduled_email')
        ->condition('eid', $record->eid)
        ->execute();
      continue;
    }
    $webform_submission = $webform_submissions[$record->sid];
    $handler_id = $record->handler_id;
    switch ($record->state) {
      case WebformScheduledEmailManagerInterface::SUBMISSION_SCHEDULE:
      case WebformScheduledEmailManagerInterface::SUBMISSION_RESCHEDULE:
        $email_status = $this
          ->schedule($webform_submission, $handler_id);
        $stats[$email_status]++;
        break;
      case WebformScheduledEmailManagerInterface::SUBMISSION_UNSCHEDULE:
        $this
          ->unschedule($webform_submission, $handler_id);
        $stats[WebformScheduledEmailManagerInterface::EMAIL_UNSCHEDULED]++;
        break;
    }
  }
  return $stats;
}