You are here

public function SiteAlertStorage::getCacheMaxAge in Site Alert 8

Returns the max age for elements that display site alerts.

Return value

int The time in seconds until the next alert is scheduled to appear or disappear, or -1 if there are no scheduled alerts.

Overrides SiteAlertStorageInterface::getCacheMaxAge

File

src/SiteAlertStorage.php, line 18

Class

SiteAlertStorage
Entity storage handler for SiteAlert entities.

Namespace

Drupal\site_alert

Code

public function getCacheMaxAge() {
  $current_datetime = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
  $query = 'SELECT MIN(t) AS t FROM (SELECT scheduling__value t FROM {' . $this
    ->getBaseTable() . '} WHERE scheduling__value > :current_time AND active = :active UNION SELECT scheduling__end_value FROM {' . $this
    ->getBaseTable() . '} WHERE scheduling__end_value > :current_time AND active = :active) AS u;';
  $next_scheduled_datetime = $this->database
    ->query($query, [
    ':current_time' => $current_datetime
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
    ':active' => SiteAlertStorageInterface::ACTIVE,
  ])
    ->fetchField();

  // If there are no scheduled alerts, there is no need to expire the cache.
  if (empty($next_scheduled_datetime)) {
    return CacheBackendInterface::CACHE_PERMANENT;
  }

  // Return the remaining time in seconds, making sure that we return minimum
  // 1 second so we will not accidentally make the element uncacheable.
  $current_unix = (int) $current_datetime
    ->format('U');
  $next_scheduled_unix = (int) (new DrupalDateTime($next_scheduled_datetime, DateTimeItemInterface::STORAGE_TIMEZONE))
    ->format('U');
  return max(1, $next_scheduled_unix - $current_unix);
}