You are here

public function ActiveSiteAlertsCacheContext::getContext in Site Alert 8

Returns the string representation of the cache context.

A cache context service's name is used as a token (placeholder) cache key, and is then replaced with the string returned by this method.

Return value

string The string representation of the cache context.

Overrides CacheContextInterface::getContext

File

src/Cache/Context/ActiveSiteAlertsCacheContext.php, line 78

Class

ActiveSiteAlertsCacheContext
A cache context that varies by the currently active site alerts.

Namespace

Drupal\site_alert\Cache\Context

Code

public function getContext() {

  // Due to cache metadata bubbling this can be called often. Only compute the
  // hash once.
  if (empty($this->hash)) {

    // Retrieve the IDs of the currently active site alerts.
    $ids = $this->getAlerts
      ->getActiveAlertIds();

    // Return a human readable string if there are no active alerts.
    if (empty($ids)) {
      return self::NO_ACTIVE_ALERTS;
    }

    // Sort the IDs, so that the same key can be generated even if the IDs
    // would be returned in a different order.
    sort($ids);

    // Generate a hash that uniquely identifies the currently active alerts.
    $this->hash = hash('sha256', $this->privateKey
      ->get() . Settings::getHashSalt() . serialize($ids));
  }
  return $this->hash;
}