Skip to content

Commit

Permalink
Adding Gravatar profiles
Browse files Browse the repository at this point in the history
Closes: #9
  • Loading branch information
arcanedev-maroc committed Feb 28, 2019
1 parent 7e98cc7 commit d2c05b4
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 21 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php

sudo: false

php:
- 7.1.3
- 7.1
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
"type": "library",
"license": "MIT",
"require": {
"php" : ">=7.1.3",
"arcanedev/support": "~4.4.0",
"arcanedev/php-html": "^1.0"
"php" : ">=7.1.3",
"ext-curl": "*",
"arcanedev/support": "~4.4.0",
"arcanedev/php-html": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~7.0",
"orchestra/testbench": "~3.7.0",
"phpunit/phpcov": "~5.0",
"orchestra/testbench": "~3.7.0"
"phpunit/phpunit": "~7.0"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 27 additions & 0 deletions src/Concerns/HashEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace Arcanedev\Gravatar\Concerns;

/**
* Trait HashEmail
*
* @package Arcanedev\Gravatar\Concerns
* @author ARCANEDEV <[email protected]>
*/
trait HashEmail
{
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Get a hashed email.
*
* @param string $email
*
* @return string
*/
public static function hashEmail($email)
{
return hash('md5', strtolower(trim($email)));
}
}
12 changes: 11 additions & 1 deletion src/Contracts/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ public function get($email, $hash = true);
*/
public function image($email, $alt = null, array $attributes = [], $rating = null);

/**
* Get profile's data.
*
* @param string $email
* @param mixed|null $default
*
* @return array|mixed
*/
public function profile($email, $default = null);

/**
* Enable the use of the secure protocol for image URLs.
*
Expand Down Expand Up @@ -142,5 +152,5 @@ public function exists($email);
*
* @return string
*/
public function hashEmail($email);
public static function hashEmail($email);
}
29 changes: 29 additions & 0 deletions src/Exceptions/InvalidProfileFormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Arcanedev\Gravatar\Exceptions;

/**
* Class InvalidProfileFormatException
*
* @package Arcanedev\Gravatar\Exceptions
* @author ARCANEDEV <[email protected]>
*/
class InvalidProfileFormatException extends \InvalidArgumentException
{
/**
* Make a new exception.
*
* @param string $format
* @param array $supportedFormat
*
* @return \Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException
*/
public static function make($format, array $supportedFormat)
{
return new static(
sprintf(
'The format [%s] is invalid, the supported formats are: %s',
$format,
implode(', ', $supportedFormat)
)
);
}
}
34 changes: 21 additions & 13 deletions src/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class Gravatar implements Contracts\Gravatar
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/

use Concerns\HashEmail;

/* -----------------------------------------------------------------
| Constants
| -----------------------------------------------------------------
Expand Down Expand Up @@ -250,7 +257,7 @@ public function get($email, $hash = true)
$url = $this->isSecured() ? static::SECURE_URL : static::BASE_URL;
$url .= empty($email)
? str_repeat('0', 32)
: ($hash ? $this->hashEmail($email) : $email);
: ($hash ? static::hashEmail($email) : $email);

$params = $this->getParams($email);

Expand Down Expand Up @@ -283,6 +290,19 @@ public function image($email, $alt = null, array $attributes = [], $rating = nul
->attributes($attributes);
}

/**
* Get profile's data.
*
* @param string $email
* @param mixed|null $default
*
* @return array|mixed
*/
public function profile($email, $default = null)
{
return (new Profile)->get($email, $default);
}

/**
* Enable the use of the secure protocol for image URLs.
*
Expand Down Expand Up @@ -323,18 +343,6 @@ public function exists($email)
return strpos($headers[0], '200') ? true : false;
}

/**
* Get a hashed email.
*
* @param string $email
*
* @return string
*/
public function hashEmail($email)
{
return hash('md5', strtolower(trim($email)));
}

/* -----------------------------------------------------------------
| Check Methods
| -----------------------------------------------------------------
Expand Down
158 changes: 158 additions & 0 deletions src/Profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php namespace Arcanedev\Gravatar;

use Arcanedev\Gravatar\Exceptions\InvalidProfileFormatException;

/**
* Class Profile
*
* @package Arcanedev\Gravatar
* @author ARCANEDEV <[email protected]>
*/
class Profile
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/

use Concerns\HashEmail;

/* -----------------------------------------------------------------
| Constants
| -----------------------------------------------------------------
*/

const BASE_URL = 'http://www.gravatar.com/';
const SECURE_URL = 'https://www.gravatar.com/';

/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/**
* Profile's format.
*
* @var string
*/
protected $format;

/**
* Supported format.
*
* @var array
*/
protected static $supportedFormat = ['json', 'xml', 'php', 'vcf', 'qr'];

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Get the profile's format.
*
* @return string|null
*/
public function getFormat()
{
return $this->format;
}

/**
* Set the profile's format.
*
* @param string $format
*
* @return \Arcanedev\Gravatar\Profile
*/
public function setFormat($format = null)
{
if ( ! is_null($format)) {
self::checkFormat($format);
$this->format = $format;
}

return $this;
}

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Build the profile URL based on the provided email address.
*
* @param string $email
* @param array $params
* @param bool $secure
*
* @return string
*/
public function getUrl($email = null, array $params = [], $secure = true)
{
$url = $secure ? static::SECURE_URL : static::BASE_URL;
$url .= is_null($email)
? str_repeat('0', 32)
: static::hashEmail($email);

if ($this->hasFormat())
$url .= ".{$this->getFormat()}";

if ( ! empty($params))
$url .= '?'.http_build_query($params);

return $url;
}

/**
* Get the profile data.
*
* @param string $email
* @param mixed|null $default
*
* @return array|mixed
*/
public function get($email, $default = null)
{
$this->setFormat('php');

$data = unserialize(
file_get_contents($this->getUrl($email))
);

return (is_array($data) && isset($data['entry']))
? $data
: $default;
}

/* -----------------------------------------------------------------
| Check Methods
| -----------------------------------------------------------------
*/

/**
* Check if the format is not null.
*
* @return bool
*/
public function hasFormat()
{
return ! is_null($this->format);
}

/**
* Check the format.
*
* @param string $format
*/
private static function checkFormat(&$format)
{
$format = strtolower($format);

if ( ! in_array($format, static::$supportedFormat)) {
throw InvalidProfileFormatException::make($format, static::$supportedFormat);
}
}
}
10 changes: 10 additions & 0 deletions tests/GravatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public function it_can_set_size_from_height_or_width_attributes()
static::assertSame(256, $this->gravatar->getSize());
}

/** @test */
public function it_get_profile()
{
$data = $this->gravatar->profile('[email protected]');

static::assertIsArray($data);
static::assertArrayHasKey('entry', $data);
static::assertCount(1, $data['entry']);
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
Expand Down
Loading

0 comments on commit d2c05b4

Please sign in to comment.