Skip to content
This repository has been archived by the owner on Mar 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #40 from alister/feature/profiles
Browse files Browse the repository at this point in the history
Gravatar Profile URL support
  • Loading branch information
henrikbjorn committed Oct 2, 2015
2 parents 4e3bd27 + 30cfffd commit 735c520
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 0 deletions.
30 changes: 30 additions & 0 deletions GravatarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,36 @@ public function getUrlForHash($hash, $size = null, $rating = null, $default = nu
return ($secure ? 'https://secure' : 'http://www').'.gravatar.com/avatar/'.$hash.'?'.http_build_query(array_filter($map));
}

/**
* Returns a url for a gravatar profile.
*
* @param string $email
* @param Boolean $secure
*
* @return string
*/
public function getProfileUrl($email, $secure = null)
{
$hash = md5(strtolower(trim($email)));

return $this->getProfileUrlForHash($hash, $secure);
}

/**
* Returns a url for a gravatar profile for the given hash.
*
* @param string $hash
* @param Boolean $secure
*
* @return string
*/
public function getProfileUrlForHash($hash, $secure = null)
{
$secure = $secure ?: $this->defaults['secure'];

return ($secure ? 'https://secure' : 'http://www').'.gravatar.com/'.$hash;
}

/**
* Checks if a gravatar exists for the email. It does this by checking for the presence of 404 in the header
* returned. Will return null if fsockopen fails, for example when the hostname cannot be resolved.
Expand Down
16 changes: 16 additions & 0 deletions Templating/Helper/GravatarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public function getUrlForHash($hash, $size = null, $rating = null, $default = nu
return $this->api->getUrlForHash($hash, $size, $rating, $default, $this->isSecure($secure));
}

/**
* {@inheritdoc}
*/
public function getProfileUrl($email, $secure = null)
{
return $this->api->getProfileUrl($email, $this->isSecure($secure));
}

/**
* {@inheritdoc}
*/
public function getProfileUrlForHash($hash, $secure = null)
{
return $this->api->getProfileUrlForHash($hash, $this->isSecure($secure));
}

public function render($email, array $options = array())
{
$size = isset($options['size']) ? $options['size'] : null;
Expand Down
20 changes: 20 additions & 0 deletions Templating/Helper/GravatarHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ public function getUrl($email, $size = null, $rating = null, $default = null, $s
*/
public function getUrlForHash($hash, $size = null, $rating = null, $default = null, $secure = null);

/**
* Returns a url for a gravatar profile.
*
* @param string $email
* @param Boolean $secure
*
* @return string
*/
public function getProfileUrl($email, $secure = null);

/**
* Returns a url for a gravatar profile, for the given hash.
*
* @param string $hash
* @param Boolean $secure
*
* @return string
*/
public function getProfileUrlForHash($hash, $secure = null);

/**
* Returns true if a avatar could be found for the email.
*
Expand Down
12 changes: 12 additions & 0 deletions Tests/GravatarApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public function testGravatarUrlWithDefaultImage()
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g&d=mm', $api->getUrl('[email protected]', 80, 'g', 'mm'));
}

public function testGravatarSecureProfileUrlWithDefaultOptions()
{
$api = new GravatarApi();
$this->assertEquals('https://secure.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee', $api->getProfileUrl('[email protected]', true));
}

public function testGravatarProfileUrlWithDefaultImage()
{
$api = new GravatarApi();
$this->assertEquals('http://www.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee', $api->getProfileUrl('[email protected]'));
}

public function testGravatarInitializedWithOptions()
{
$api = new GravatarApi(array(
Expand Down
29 changes: 29 additions & 0 deletions Tests/Templating/Helper/GravatarHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ public function testGetUrlReturnsTheCorrectSecureUrl()
);
}

public function testGetProfileUrlReturnsTheCorrectUrl()
{
$this->assertEquals('http://www.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee', $this->helper->getProfileUrl('[email protected]'));
}

public function testGetProfileUrlReturnsTheCorrectSecureUrl()
{
$this->assertEquals(
'https://secure.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee',
$this->helper->getProfileUrl('[email protected]', true)
);
}

public function testGetProfileUrlForHashReturnsTheCorrectUrl()
{
$this->assertEquals(
'http://www.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee',
$this->helper->getProfileUrlForHash('0aa61df8e35327ac3b3bc666525e0bee')
);
}

public function testGetProfileUrlForHashReturnsTheCorrectSecureUrl()
{
$this->assertEquals(
'https://secure.gravatar.com/0aa61df8e35327ac3b3bc666525e0bee',
$this->helper->getProfileUrlForHash('0aa61df8e35327ac3b3bc666525e0bee', true)
);
}

public function testCheckForAvatarExistance()
{
$this->assertTrue($this->helper->exists('[email protected]'));
Expand Down
4 changes: 4 additions & 0 deletions Tests/Twig/GravatarExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function testProxyMethods()
$this->extension->getUrl('[email protected]');
$this->extension->exists('[email protected]');
$this->extension->getUrlForHash(md5('[email protected]'));
$this->extension->getProfileUrl('[email protected]');
$this->extension->getProfileUrlForHash(md5('[email protected]'));
}

public function testName()
Expand All @@ -50,6 +52,8 @@ public function testFunctions()
$expectedNames = array(
'gravatar',
'gravatar_hash',
'gravatar_profile',
'gravatar_profile_hash',
'gravatar_exists',
);

Expand Down
18 changes: 18 additions & 0 deletions Twig/GravatarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function getFunctions()
return array(
new \Twig_SimpleFunction('gravatar', array($this, 'getUrl')),
new \Twig_SimpleFunction('gravatar_hash', array($this, 'getUrlForHash')),
new \Twig_SimpleFunction('gravatar_profile', array($this, 'getProfileUrl')),
new \Twig_SimpleFunction('gravatar_profile_hash', array($this, 'getProfileUrlForHash')),
new \Twig_SimpleFunction('gravatar_exists', array($this, 'exists')),
);
}
Expand All @@ -48,6 +50,22 @@ public function getUrlForHash($hash, $size = null, $rating = null, $default = nu
return $this->baseHelper->getUrlForHash($hash, $size, $rating, $default, $secure);
}

/*
* {@inheritdoc}
*/
public function getProfileUrl($email, $secure = null)
{
return $this->baseHelper->getProfileUrl($email, $secure);
}

/**
* {@inheritdoc}
*/
public function getProfileUrlForHash($hash, $secure = null)
{
return $this->baseHelper->getProfileUrlForHash($hash, $secure);
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 735c520

Please sign in to comment.