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

Commit

Permalink
Added PHPDoc to GravatarHelper. GravatarApi is know injected into the…
Browse files Browse the repository at this point in the history
… GravatarHelper. Created a service for GravatarApi to make it configurable. Removed static from GravatarApi::getUrl so the class can be configured when intialized
  • Loading branch information
henrikbjorn authored and ornicar committed Aug 17, 2010
1 parent 0c9b720 commit 4c2c0e8
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 22 deletions.
34 changes: 27 additions & 7 deletions GravatarApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,43 @@
*/
class GravatarApi
{
/**
* @var array $default array of default options that can be overriden with getters and in the construct.
*/
protected $defaults = array(
'size' => 80,
'rating' => 'g',
'default' => null,
);

/**
* Constructor
*
* @param array $options the array is merged with the defaults.
* @return void
*/
public function __construct(array $options = array()) {
$this->defaults = array_merge($this->defaults, $options);
}

/**
* Returns a url for a gravatar.
*
* @param string $email
* @param integer $size defaults to 80
* @param string $rating defaults to g
* @param string $default defaults to null
* @param integer $size
* @param string $ratin
* @param string $default
* @return string
*/
public static function getUrl($email, $size = 80, $rating = 'g', $default = null)
public function getUrl($email, $size = null, $rating = null, $default = null)
{
$hash = md5(strtolower($email));

$options = array_merge($this->defaults, array_filter(compact('size', 'rating', 'default')));
$map = array(
's' => $size,
'r' => $rating,
'd' => $default,
's' => $options['size'],
'r' => $options['rating'],
'd' => $options['default'],
);

return 'http://www.gravatar.com/avatar/' . $hash . '?' . http_build_query(array_filter($map));
Expand Down
20 changes: 12 additions & 8 deletions Resources/config/helper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="templating.helper.gravatar.class">Bundle\GravatarBundle\Templating\Helper\GravatarHelper</parameter>
</parameters>
<parameters>
<parameter key="templating.helper.gravatar.class">Bundle\GravatarBundle\Templating\Helper\GravatarHelper</parameter>
<parameter key="gravatar.api.class">Bundle\GravatarBundle\GravatarApi</parameter>
</parameters>

<services>
<service id="templating.helper.gravatar" class="%templating.helper.gravatar.class%">
<tag name="templating.helper" alias="gravatar" />
</service>
</services>
<services>
<service id="gravatar.api" class="%gravatar.api.class%" />

<service id="templating.helper.gravatar" class="%templating.helper.gravatar.class%">
<tag name="templating.helper" alias="gravatar" />
<argument type="service" id="gravatar.api" />
</service>
</services>
</container>
30 changes: 27 additions & 3 deletions Templating/Helper/GravatarHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,39 @@

class GravatarHelper implements HelperInterface
{
/**
* @var string $charset
*/
protected $charset = 'UTF-8';

public function __construct()
/**
* @var Bundle\GravatarBundle\GravatarApi $api
*/
protected $api;

/**
* Constructor
*
* @param Bundle\GravatarBundle\GravatarApi $api
* @return void
*/
public function __construct(GravatarApi $api)
{
$this->api = $api;
}

public function render($email, $size = 80, $rating = 'g', $default = null)
/**
* Returns a url for a gravatar
*
* @param string $email
* @param integer $size
* @param string $rating
* @param string $default
* @return string
*/
public function render($email, $size = null, $rating = null, $default = null)
{
return GravatarApi::getUrl($email, $size, $rating, $default);
return $this->api->getUrl($email, $size, $rating, $default);
}

/**
Expand Down
17 changes: 14 additions & 3 deletions Tests/GravatarApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,26 @@

class GravatarApiTest extends TestCase
{

public function testGravatarUrlWithDefaultOptions()
{
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', GravatarApi::getUrl('henrik@bearwoods.dk'));
$api = new GravatarApi();
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g', $api->getUrl('henrik@bearwoods.dk'));
}

public function testGravatarUrlWithDefaultImage()
{
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g&d=mm', GravatarApi::getUrl('henrik@bearwoods.dk', 80, 'g', 'mm'));
$api = new GravatarApi();
$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=80&r=g&d=mm', $api->getUrl('henrik@bearwoods.dk', 80, 'g', 'mm'));
}

public function testGravatarInitializedWithOptions()
{
$api = new GravatarApi(array(
'size' => 20,
'default' => 'mm',
));

$this->assertEquals('http://www.gravatar.com/avatar/0aa61df8e35327ac3b3bc666525e0bee?s=20&r=g&d=mm', $api->getUrl('henrik@bearwoods.dk'));
}

}
2 changes: 1 addition & 1 deletion Tests/Templating/Helper/GravatarHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GravatarHelperTest extends TestCase

public function setUp()
{
$this->helper = new GravatarHelper();
$this->helper = new GravatarHelper(new GravatarApi());
}

public function testRenderReturnsTheCorrectUrl()
Expand Down

0 comments on commit 4c2c0e8

Please sign in to comment.