Skip to content

Commit

Permalink
Added Gravatar services.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpi committed Oct 1, 2017
1 parent aae4bf1 commit f898945
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types = 1);

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

use dpi\ak\AvatarIdentifierInterface;
use dpi\ak\AvatarKit\AvatarServices\AvatarServiceBase;
use dpi\ak_gravatar\GravatarAvatarIdentifier;
use League\Uri\Components\Query;
use League\Uri\Schemes\Http;

/**
* Common functionality for Gravatar services.
*/
abstract class GravatarBase extends AvatarServiceBase implements GravatarInterface {

/**
* {@inheritdoc}
*/
public function getAvatar(AvatarIdentifierInterface $identifier) : string {
$components = [];

$components['scheme'] = $this->getConfiguration()->getProtocol();
// @todo test invalid protocol (this shouldnt happen since factory validates).
$components['host'] = $components['scheme'] == 'http' ? 'gravatar.com' : 'secure.gravatar.com';

// Path.
$identifier = $identifier->getHashed();
$components['path'] = '/avatar/' . $identifier;

// Query.
$width = $this->getConfiguration()->getWidth();
$query = [];
if ($width) {
$query['s'] = $width;
}
if ($query) {
$components['query'] = Query::build($query);
}

return (string) Http::createFromComponents($components);
}

/**
* {@inheritdoc}
*/
public function createIdentifier() : AvatarIdentifierInterface {
return (new GravatarAvatarIdentifier())
->setHasher(['\dpi\ak_gravatar\GravatarAvatarIdentifier', 'gravatarHasher']);
}

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarIdenticon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Identicon avatar service.
*
* @AvatarService(
* id = "gravatar_identicon",
* name = "Identicon",
* description = "Identicon as provided by Gravatar.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = FALSE,
* is_fallback = TRUE,
* is_remote = TRUE
* )
*/
class GravatarIdenticon extends GravatarStaticBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return 'identicon';
}

}
23 changes: 23 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

use dpi\ak\AvatarKit\AvatarServices\AvatarServiceInterface;

/**
* Interface ServiceInterface.
*/
interface GravatarInterface extends AvatarServiceInterface {

/**
* Default fallback identifier.
*
* This identifier is used when a Universal avatar is not found, or if 'force
* default' argument is set.
*
* @return string
* The API identifier for default provider.
*/
public function defaultId();

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarMonster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Monster avatar service.
*
* @AvatarService(
* id = "gravatar_monster",
* name = "Monster",
* description = "Monster as provided by Gravatar.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = FALSE,
* is_fallback = TRUE,
* is_remote = TRUE
* )
*/
class GravatarMonster extends GravatarStaticBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return 'monsterid';
}

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarMysteryMan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Mystery Man avatar service.
*
* @AvatarService(
* id = "gravatar_mystery_man",
* name = "Mystery Man",
* description = "Mystery Man as provided by Gravatar.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = FALSE,
* is_fallback = TRUE,
* is_remote = TRUE
* )
*/
class GravatarMysteryMan extends GravatarStaticBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return 'mm';
}

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarRetro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Retro avatar service.
*
* @AvatarService(
* id = "gravatar_retro",
* name = "Retro",
* description = "Retro as provided by Gravatar.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = FALSE,
* is_fallback = TRUE,
* is_remote = TRUE
* )
*/
class GravatarRetro extends GravatarStaticBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return 'retro';
}

}
31 changes: 31 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarStaticBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types = 1);

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

use dpi\ak\AvatarIdentifierInterface;
use League\Uri\Components\Query;
use League\Uri\Schemes\Http;

/**
* Base class for static Gravatars.
*/
abstract class GravatarStaticBase extends GravatarBase implements GravatarInterface {

/**
* {@inheritdoc}
*/
public function getAvatar(AvatarIdentifierInterface $identifier) : string {
$url = Http::createFromString(parent::getAvatar($identifier));

$query = new Query($url->getQuery());
$query = $query->merge(Query::build([
'f' => 'y',
'd' => $this->defaultId(),
]));

return (string) $url->withQuery((string) $query);
}

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarUniversal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Universal Gravatar service.
*
* @AvatarService(
* id = "gravatar_universal",
* name = "Gravatar",
* description = "Universal Gravatar avatar hosting service.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = TRUE,
* is_fallback = FALSE,
* is_remote = TRUE
* )
*/
class GravatarUniversal extends GravatarBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return '404';
}

}
34 changes: 34 additions & 0 deletions src/AvatarKit/AvatarServices/Gravatar/GravatarWavatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar;

/**
* Wavatar avatar service.
*
* @AvatarService(
* id = "gravatar_wavatar",
* name = "Wavatar",
* description = "Wavatar as provided by Gravatar.",
* protocols = {
* "http",
* "https"
* },
* mime = {
* "image/jpeg"
* },
* dimensions = "1x1-2048x2048",
* is_dynamic = FALSE,
* is_fallback = TRUE,
* is_remote = TRUE
* )
*/
class GravatarWavatar extends GravatarStaticBase {

/**
* {@inheritdoc}
*/
public function defaultId() {
return 'wavatar';
}

}
43 changes: 43 additions & 0 deletions src/GravatarAvatarIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types = 1);

namespace dpi\ak_gravatar;

use dpi\ak\AvatarIdentifier;
use dpi\ak\Exception\AvatarIdentifierException;

/**
* Specifies identifier logic specific to Gravatar API.
*/
class GravatarAvatarIdentifier extends AvatarIdentifier {

/**
* {@inheritdoc}
*/
public function setHashed(string $string) {
if (strlen($string) > 32) {
// Avatars will not be unique if longer than 32 characters.
throw new AvatarIdentifierException('Gravatar does not recognise characters after 32nd character.');
}
return parent::setHashed($string);
}

/**
* Hashes strings for Gravatar.
*
* @param string $string
* The string to hash.
*
* @return string
* A hashed string.
*
* @see \dpi\ak_gravatar\AvatarKit\AvatarServices\Gravatar\GravatarBase::createIdentifier
*/
public static function gravatarHasher(string $string) {
$string = strtolower($string);
$string = md5($string);
return $string;
}

}

0 comments on commit f898945

Please sign in to comment.