Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Sep 27, 2019
0 parents commit 896f84f
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/composer.lock
/phpunit.xml
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php
dist: precise
sudo: false

php:
- 7.1
- 7.2
- 7.3

matrix:
fast_finish: true

cache:
directories:
- $HOME/.composer/cache

before_install:
- phpenv config-rm xdebug.ini || true

before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-dist

script:
- php vendor/bin/phpunit
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Dimitri Gritsajuk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Gravatar image provider for Faker

[![Build Status](https://travis-ci.org/ottaviano/faker-gravatar.svg?branch=master)](https://travis-ci.org/ottaviano/faker-gravatar)

### Requirements

- PHP >= 7.1

### Installation

```bash
composer require ottaviano/faker-gravatar --dev
```
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "ottaviano/faker-gravatar",
"license": "MIT",
"description": "Faker Gravatar image provider",
"type": "library",
"keywords": ["faker", "fixtures", "data", "gravatar", "image", "avatar"],
"authors": [
{
"name": "Dimitri Gritsajuk",
"email": "gritsajuk.dimitri@gmail.com"
}
],
"require": {
"php": "^7.1"
},
"require-dev": {
"fzaninotto/faker": "^1.6",
"phpunit/phpunit": "^7.5",
"ext-curl": "*"
},
"autoload": {
"psr-4": {
"Ottaviano\\Faker\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\Ottaviano\\Faker\\": "tests/"
}
}
}
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.4/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
70 changes: 70 additions & 0 deletions src/Gravatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Ottaviano\Faker;

use Faker\Provider\Base;

class Gravatar extends Base
{
private const MODES = [
'blank',
'identicon',
'monsterid',
'mp',
'retro',
'robohash',
'wavatar',
];

private const URL = 'https://www.gravatar.com/avatar/%s.jpg?d=%s&size=%d';

public static function gravatarUrl(string $mode = null, string $email = null, int $size = 80): string
{
if (!$mode || !in_array($mode, static::MODES, true)) {
$mode = 'retro';
}

$hash = $email ? md5(static::toLower($email)) : static::randomNumber(5, true);

return sprintf(static::URL, $hash, $mode, $size);
}

public static function gravatar(string $dir = null, string $mode = null, string $email = null, int $size = 80, bool $fullPath = true): ?string
{
$dir = $dir ?? sys_get_temp_dir();

if (!is_dir($dir) || !is_writable($dir)) {
throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
}

$name = md5(uniqid($_SERVER['SERVER_ADDR'] ?? '', true));
$filename = $name.'.jpg';
$filepath = $dir.DIRECTORY_SEPARATOR.$filename;

$url = static::gravatarUrl($mode, $email, $size);

// save file
if (function_exists('curl_exec')) {
// use cURL
$fp = fopen($filepath, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch) && 200 === curl_getinfo($ch, CURLINFO_HTTP_CODE);
fclose($fp);
curl_close($ch);

if (!$success) {
unlink($filepath);

// could not contact the distant URL or HTTP error - fail silently.
return null;
}
} elseif (ini_get('allow_url_fopen')) {
copy($url, $filepath);
} else {
return new \RuntimeException('The image formatter downloads an image from a remote HTTP server. Therefore, it requires that PHP can request remote hosts, either via cURL or fopen()');
}

return $fullPath ? $filepath : $filename;
}
}
61 changes: 61 additions & 0 deletions tests/GravatarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Test\Ottaviano\Faker;

use Ottaviano\Faker\Gravatar;
use PHPUnit\Framework\TestCase;

class GravatarTest extends TestCase
{
public function testDefaultUrlValues()
{
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=retro&size=80#', Gravatar::gravatarUrl());
}

public function testGravatarUrlAcceptsCustomMode()
{
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=mp&size=80#', Gravatar::gravatarUrl('mp'));
}

public function testGravatarUrlAcceptsCustomSize()
{
$this->assertRegExp('#^https://www\.gravatar\.com/avatar/\d+\.jpg\?d=retro&size=200#', Gravatar::gravatarUrl(null, null, 200));
}

public function testGravatarUrlAcceptsCustomEmail()
{
$email = 'TeSt@EmAiL.OK';
$hash = md5(strtolower($email));

$this->assertRegExp("#^https://www\.gravatar\.com/avatar/${hash}\.jpg\?d=retro&size=80#", Gravatar::gravatarUrl(null, $email));
}

public function testDownloadWithDefaults()
{
$curlPing = curl_init('https://www.gravatar.com');

curl_setopt($curlPing, CURLOPT_TIMEOUT, 5);
curl_setopt($curlPing, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curlPing, CURLOPT_RETURNTRANSFER, true);

curl_exec($curlPing);

$httpCode = curl_getinfo($curlPing, CURLINFO_HTTP_CODE);

curl_close($curlPing);

if ($httpCode < 200 | $httpCode > 300) {
$this->markTestSkipped('Gravatar is offline, skipping image download');
}

$file = Gravatar::gravatar();

$this->assertFileExists($file);

$this->assertEquals('jpg', pathinfo($file, PATHINFO_EXTENSION));

if (file_exists($file)) {
unlink($file);
}
}
}

0 comments on commit 896f84f

Please sign in to comment.