You are here

public function AcsfThemeNotify::processNotifications in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 src/AcsfThemeNotify.php \Drupal\acsf\AcsfThemeNotify::processNotifications()

Resends failed theme notifications.

Parameters

int $limit: The number of notification that should be processed.

Return value

int Returns the number of successfully sent notifications, or -1 if none of the pending notifications managed to get sent.

File

src/AcsfThemeNotify.php, line 141

Class

AcsfThemeNotify
Manages theme notifications that need to be sent to the Factory.

Namespace

Drupal\acsf

Code

public function processNotifications($limit) {
  if (!$this
    ->isEnabled()) {
    return -1;
  }
  $notifications = $this
    ->getNotifications($limit);

  // If there were no pending notifications then we can consider this process
  // successful.
  $success = 0;
  foreach ($notifications as $notification) {

    // If this is a notification for an event that is not supported, it will
    // never get a 200 response so we need to remove it from storage.
    if (!in_array($notification->event, [
      'create',
      'modify',
      'delete',
    ])) {
      $this
        ->removeNotification($notification);
      continue;
    }

    // Increment the count of attempts on this notification. At the first pass
    // through this function, this notification has already been attempted
    // once.
    $this
      ->incrementNotificationAttempts($notification);

    // Remove notification and handle if it exceeds the maximum allowed
    // attempts (default 3). The assumption behind the >= comparison here is
    // that the notification was already tried once before it was stored in
    // the table.
    if ($notification->attempts >= $this->acsfVarStorage
      ->get('acsf_theme_notification_max_attempts', 3)) {
      $this
        ->removeNotification($notification);

      // @todo Any additional handling needed? DG-11826
    }

    // Only "site" or "theme" notifications get stored. Any notification with
    // a non-empty theme field is assumed to be a theme notification,
    // otherwise it is a site notification.
    $scope = !empty($notification->theme) ? 'theme' : 'site';

    // Try to send the notification but if it fails do not store it again.
    $response = $this
      ->sendNotification($scope, $notification->event, NULL, $notification->theme, $notification->timestamp, FALSE);
    if ($response['code'] === 200) {
      $this
        ->removeNotification($notification);
      $success++;
    }
  }
  return $success == 0 && !empty($notifications) ? -1 : $success;
}