Skip to content

Commit

Permalink
[TASK][DEV-500] Avatars with md5 hashes (#10)
Browse files Browse the repository at this point in the history
* [TASK] Unify t3g composer commands

* [TASK] Allow md5 hash in avatar utility

* [TASK] Add test for avatar utility
  • Loading branch information
Woeler authored Jun 25, 2020
1 parent 88374ae commit 432cb6f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ jobs:
run: yarn install && yarn build

- name: Lint
run: composer test:php:lint
run: composer t3g:test:php:lint

- name: CGL
run: composer test:php:cgl
run: composer t3g:cgl

- name: Unit Tests
run: composer test:php:unit
run: composer t3g:test:php:unit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor
*.idea
*.lock
*Zone.Identifier
/.phplint-cache
18 changes: 11 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@
"vendor-dir": "vendor"
},
"scripts": {
"test:php:unit": [
"t3g:test:php:unit": [
"simple-phpunit -c phpunit.xml.dist --testsuite \"Unit Test Suite\""
],
"test:php:cgl": [
"php-cs-fixer --diff -v fix"
"t3g:cgl": [
"php-cs-fixer fix -v --dry-run"
],
"test:php:lint": [
"t3g:cgl:fix": [
"php-cs-fixer fix"
],
"t3g:test:php:lint": [
"phplint"
],
"test": [
"@test:php:cgl",
"@test:php:lint"
"t3g:test": [
"@t3g:cgl",
"@t3g:test:php:lint",
"@t3g:test:php:unit"
]
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions src/Twig/Extension/AvatarExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function getFunctions(): array
];
}

public function render(string $email, int $size = 36): string
public function render(string $value, int $size = 36): string
{
return AvatarUtility::getAvatar((string) $email, (int) $size);
return AvatarUtility::getAvatar($value, $size);
}

public function getName(): string
Expand Down
24 changes: 14 additions & 10 deletions src/Utility/AvatarUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,38 @@
class AvatarUtility
{
/**
* @param string $email
* @param string $value
* @param int $size
* @return string
*/
public static function getAvatar($email, $size = 80): string
public static function getAvatar(string $value, int $size = 80): string
{
$attributes = '';

$attributesData = [];
$attributesData['src'] = self::getAvatarUrl($email, $size);
$attributesData['src'] = self::getAvatarUrl($value, $size);
$attributesData['class'] = 'avatar';
$attributesData['height'] = (int) $size;
$attributesData['width'] = (int) $size;
$attributesData['height'] = $size;
$attributesData['width'] = $size;

foreach ($attributesData as $key => $value) {
$attributes .= ' ' . $key . '="' . $value . '"';
foreach ($attributesData as $key => $data) {
$attributes .= ' ' . $key . '="' . $data . '"';
}

return '<img' . $attributes . '/>';
}

/**
* @param string $email
* @param string $value
* @param int $size
* @return string
*/
public static function getAvatarUrl($email, $size = 80): string
public static function getAvatarUrl(string $value, int $size = 80): string
{
return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($email))) . '?s= ' . (int) $size . '&d=mp&r=g';
if (0 === preg_match('/^[a-f0-9]{32}$/', $value)) {
$value = md5(strtolower(trim($value)));
}

return 'https://www.gravatar.com/avatar/' . $value . '?s=' . $size . '&d=mp&r=g';
}
}
1 change: 0 additions & 1 deletion tests/Unit/DependencyInjection/TemplateExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Yaml\Parser;
use T3G\Bundle\TemplateBundle\DependencyInjection\TemplateExtension;

class TemplateExtensionTest extends TestCase
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Utility/AvatarUtilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

/*
* This file is part of the package t3g/symfony-template-bundle.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace T3G\Bundle\TemplateBundle\Tests\Unit\Utility;

use PHPUnit\Framework\TestCase;
use T3G\Bundle\TemplateBundle\Utility\AvatarUtility;

class AvatarUtilityTest extends TestCase
{
/**
* @dataProvider avatarDataProvider
* @param string $value
* @param string $expected
*/
public function testGetAvatarUrl(string $value, string $expected): void
{
$this->assertEquals($expected, AvatarUtility::getAvatarUrl($value));
}

public function avatarDataProvider(): array
{
return [
[
'value' => 'hello@world.com',
'expected' => 'https://www.gravatar.com/avatar/4b3cdf9adfc6258a102ab90eb64565ea?s=80&d=mp&r=g',
],
[
'value' => '4b3cdf9adfc6258a102ab90eb64565ea',
'expected' => 'https://www.gravatar.com/avatar/4b3cdf9adfc6258a102ab90eb64565ea?s=80&d=mp&r=g',
]
];
}
}

0 comments on commit 432cb6f

Please sign in to comment.