Skip to content

Commit

Permalink
Gravatar function
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Feb 21, 2016
1 parent 49b1b55 commit 4323847
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"pimple/pimple" : "^3.0",
"jbzoo/sqlbuilder" : "^1.0",
"jbzoo/data" : "^1.3.4",
"jbzoo/utils" : "^1.2.1",
"jbzoo/utils" : "^1.4.3",
"jbzoo/path" : "^1.0",
"jbzoo/event" : "^1.2.1"
},
Expand Down
34 changes: 34 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
namespace JBZoo\CrossCMS\Entity;

use JBZoo\Data\Data;
use JBZoo\Utils\Arr;
use JBZoo\Utils\Url;

/**
* Class AbstractAssets
Expand Down Expand Up @@ -112,4 +114,36 @@ public function isGuest()
{
return 0 === $this->_id;
}

/**
* Get user avatar by email
*
* @param int $avatarSize
* @param string $defaultPic
* @return string
*/
public function getAvatar($avatarSize = 64, $defaultPic = 'identicon')
{
$md5 = md5(trim($this->_email));
$avatarSize = (int)$avatarSize;
$defaultPic = trim($defaultPic);

$validList = array('404', 'mm', 'identicon', 'monsterid', 'wavatar', 'retro', 'blank');

if (strpos($defaultPic, 'http') === 0) {
$default = urlencode($defaultPic);
} elseif (Arr::in((string)$defaultPic, $validList)) {
$default = $defaultPic;
} else {
$default = 'identicon';
}

if (Url::isHttps()) {
$avatarUrl = 'https://secure.gravatar.com/avatar/' . $md5 . '.jpg?s=' . $avatarSize . '&d=' . $default;
} else {
$avatarUrl = 'http://www.gravatar.com/avatar/' . $md5 . '.jpg?s=' . $avatarSize . '&d=' . $default;
}

return $avatarUrl;
}
}
32 changes: 32 additions & 0 deletions tests/tests/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,36 @@ public function testUndefinedUser()
isSame(null, $helper->getByLogin(uniqid()));
isSame(null, $helper->getByEmail(uniqid()));
}

public function testGravatar()
{
$helper = $this->_getUser();

$user = $helper->getCurrent();
isSame(
'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?s=64&d=identicon',
$user->getAvatar()
);

isSame(
'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?s=128&d=identicon',
$user->getAvatar(128)
);

isSame(
'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?s=128&d=identicon',
$user->getAvatar(128, 'qwerty')
);

isSame(
'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?s=128&d=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg',
$user->getAvatar(128, 'http://example.com/images/avatar.jpg')
);

$_SERVER['HTTPS'] = 'on';
isSame(
'https://secure.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg?s=128&d=mm',
$user->getAvatar(128, 'mm')
);
}
}

0 comments on commit 4323847

Please sign in to comment.