You are here

public function Gravatar::getUrl in Avatar Kit 8

Gets the URL for the avatar.

Return value

string A URL for an avatar.

Throws

\Drupal\avatars\Exception\AvatarException Thrown if missing parameters.

Overrides AvatarBaseInterface::getUrl

File

avatars_gravatar/src/Gravatar.php, line 189

Class

Gravatar
Implements the Gravatar.com API.

Namespace

Drupal\avatars_gravatar

Code

public function getUrl() {
  $kv = [];
  $url = ($this
    ->isSecure() ? 'https://' : 'http://') . $this
    ->getHostName() . '/avatar/';
  $identifier = $this
    ->getIdentifier();
  if (!strlen($identifier)) {
    throw new AvatarException('Missing avatar identifier/hash');
  }
  $url .= $this
    ->identifierIsPreHashed() ? $identifier : $this
    ->hashIdentifier($identifier);
  $type = $this
    ->getType();
  if (!in_array($type, $this
    ->getFallbackTypes())) {
    if (isset($this->fallbackType)) {
      $kv['d'] = $this->fallbackType;
    }
    elseif (isset($this->fallbackURI)) {

      // Fallback URI is already urlencode'd by http_build_query().
      $kv['d'] = $this->fallbackURI;
    }
  }
  else {
    $type_map = $this
      ->getTypesMap();
    if (!empty($type)) {
      $kv['d'] = $type_map[$type];
      $kv['f'] = 'y';
    }
  }
  if (is_numeric($this->width)) {
    $kv['s'] = $this->width;
  }
  if (isset($this->rating)) {
    $kv['r'] = $this->rating;
  }
  $query = http_build_query($kv);
  return !empty($query) ? $url . '?' . $query : $url;
}