maba/avatar-bundle

Symfony2 bundle for getting and displaying user's avatar

Installs: 544

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 1

Open Issues: 0

Type:symfony-bundle

0.1.0 2015-07-26 00:31 UTC

This package is auto-updated.

Last update: 2024-12-10 21:04:11 UTC


README

What's that?

Symfony2 bundle to get avatar image for given email.

It lets to register additional avatar providers so that most appropriate avatar can be selected.

It includes one default avatar provider - GravatarProvider.

Installing

composer require maba/avatar-bundle

Inside AppKernel::registerBundles:

new Maba\Bundle\AvatarBundle\MabaAvatarBundle(),

Configuring

maba_avatar:
    default_uri: /assets/unknown.png    # defaults to ~
    default_size: 50
    gravatar:
        enabled: true
        secure: false
        force_default: false
        default: mm             # one of mm, 404, identicon, monsterid, wavatar, retro, blank
                                # ignored if default_uri is set
        rating: ~               # one of g, pg, r, x

Adding avatar provider

  1. Make class which implements Maba\Bundle\AvatarBundle\Service\AvatarProviderInterface.
  2. Register it as a service.
  3. Add tag maba_avatar.avatar_provider to that service.
  4. Optionally provide priority attribute to that tag. Smallest number means provider will be called first. GravatarProvider has priority 9000, but always returns URI, so your priorities should be smaller than that. If not provided, defaults to 0.

Example:

namespace Acme;

use Maba\Bundle\AvatarBundle\Service\AvatarProviderInterface;

class MyAvatarProvider implements AvatarProviderInterface
{
    // ...
    
    public function getAvatar($email, $size)
    {
        $user = $this->repository->findOneByEmail($email);
        if ($user === null) {
            // we don't have avatar - other providers will by called by priority
            return null;
        }
        
        return $this->avatarPath . $user->getAvatar();
    }
    
    // ...
}
<service id="acme.my_avatar_provider" class="Acme\MyAvatarProvider">
    <tag name="maba_avatar.avatar_provider" priority="0"/>
    <!-- any other configuration -->
</service>

Running tests

Travis status Coverage Status

composer install
vendor/bin/phpunit