<?php namespace App\Modules\Utils\Gravatar;

use \Config;
class Gravatar
{

	const HTTP_URL 	= 'http://www.gravatar.com/avatar/';
	const HTTPS_URL = 'https://secure.gravatar.com/avatar/';

	protected $usingSecureUrl = false;

	public function __construct()
    {
    	$this->usingSecureUrl = Config::get('gravatar::using-https');
    }

	public function url($email, $size = '60')
	{
		$d = 'mm';
		$r = 'g';
		$img = false;
		$atts = array();

		if($this->usingSecureUrl)
			$url = static::HTTPS_URL;
		else
			$url = static::HTTP_URL;


	    $url .= md5( strtolower( trim( $email ) ) );
	    $url .= "?s=$size&d=$d&r=$r";
	    if ( $img ) {
	        $url = '<img src="' . $url . '"';
	        foreach ( $atts as $key => $val )
	            $url .= ' ' . $key . '="' . $val . '"';
	        $url .= ' />';
	    }
	    return $url;
	}
}