Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove caching #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class Avatar

protected string $initials = '';

protected Repository|ArrayStore $cache;

protected mixed $driver;

protected GeneratorInterface $initialGenerator;
Expand All @@ -80,11 +78,9 @@ class Avatar
* Avatar constructor.
*
* @param array $config
* @param Repository $cache
*/
public function __construct(array $config = [], Repository $cache = null)
public function __construct(array $config = [])
{
$this->cache = $cache ?? new ArrayStore();
$this->driver = $config['driver'] ?? 'gd';
$this->theme = $config['theme'] ?? null;
$this->defaultTheme = $this->validateConfig($config);
Expand Down Expand Up @@ -180,17 +176,10 @@ protected function resolveTheme(array|string|null $theme, array $cfg): array

public function toBase64(): string
{
$key = $this->cacheKey();
if ($base64 = $this->cache->get($key)) {
return $base64;
}

$this->buildAvatar();

$base64 = $this->image->toPng()->toDataUri();

$this->cache->forever($key, $base64);

return $base64;
}

Expand Down Expand Up @@ -389,28 +378,6 @@ function (RectangleFactory $draw) use ($width, $height) {
);
}

protected function cacheKey(): string
{
$keys = [];
$attributes = [
'name',
'initials',
'shape',
'chars',
'font',
'fontSize',
'width',
'height',
'borderSize',
'borderColor',
];
foreach ($attributes as $attr) {
$keys[] = $this->$attr;
}

return md5(implode('-', $keys));
}

/**
* @throws \Random\RandomException
*/
Expand Down
3 changes: 1 addition & 2 deletions src/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public function register()
{
$this->app->bind('avatar', function (Application $app) {
$config = $app->make('config');
$cache = $app->make('cache.store');

$avatar = new Avatar($config->get('laravolt.avatar'), $cache);
$avatar = new Avatar($config->get('laravolt.avatar'));
$avatar->setGenerator($app['avatar.generator']);

return $avatar;
Expand Down
3 changes: 1 addition & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ class ServiceProvider extends BaseServiceProvider
public function register()
{
$this->app->bind('avatar', function (Application $app) {
$cache = $app->make('cache.store');
$config = $app['config']->get('laravolt.avatar', []);

$avatar = new Avatar($config, $cache);
$avatar = new Avatar($config);
$avatar->setGenerator($app['avatar.generator']);

return $avatar;
Expand Down
27 changes: 12 additions & 15 deletions tests/AvatarLaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ public function it_can_override_attributes_when_instantiated()
'border' => ['size' => 1, 'color' => '#999999', 'radius' => 15],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('make')->andReturn('AB');
$generator->shouldReceive('setUppercase');
$generator->shouldReceive('setAscii');

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar = new \Laravolt\Avatar\Avatar($config);
$avatar->setGenerator($generator);

$this->assertEquals(2, $avatar->getAttribute('chars'));
$this->assertEquals('circle', $avatar->getAttribute('shape'));
Expand All @@ -56,14 +55,13 @@ public function it_have_no_border_radius_as_default()
'border' => ['size' => 1, 'color' => '#999999'],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('make')->andReturn('AB');
$generator->shouldReceive('setUppercase');
$generator->shouldReceive('setAscii');

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar = new \Laravolt\Avatar\Avatar($config);
$avatar->setGenerator($generator);

$this->assertEquals(0, $avatar->getAttribute('borderRadius'));
}
Expand All @@ -73,7 +71,6 @@ public function it_have_no_border_radius_as_default()
*/
public function it_can_override_attributes_after_set_name()
{
$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');
$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('setName')->andReturnSelf();
$generator->shouldReceive('setLength');
Expand All @@ -83,7 +80,8 @@ public function it_can_override_attributes_after_set_name()
$generator->shouldReceive('base_path');
$config = ['backgrounds' => ['#000000', '#111111'], 'foregrounds' => ['#EEEEEE', '#FFFFFF']];

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar = new \Laravolt\Avatar\Avatar($config);
$avatar->setGenerator($generator);
$avatar->create('A');

$this->assertEquals('#FFFFFF', $avatar->getAttribute('foreground'));
Expand All @@ -99,13 +97,12 @@ public function it_has_correct_random_background()
'backgrounds' => ['#111111', '#000000'],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('setUppercase');
$generator->shouldReceive('setAscii');

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar = new \Laravolt\Avatar\Avatar($config);
$avatar->setGenerator($generator);

$name = 'A';

Expand All @@ -128,8 +125,6 @@ public function it_has_different_random_background()
'backgrounds' => ['#000000', '#111111'],
];

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('setUppercase');
$generator->shouldReceive('setAscii');
Expand All @@ -141,12 +136,14 @@ public function it_has_different_random_background()
$generator->shouldReceive('setName')->andReturn($name1);
$generator->shouldReceive('make')->andReturn('AA');

$avatar1 = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar1 = new \Laravolt\Avatar\Avatar($config);
$avatar1->setGenerator($generator);
$avatar1->create($name1)->buildAvatar();

$generator->shouldReceive('setName')->andReturn($name2);

$avatar2 = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar2 = new \Laravolt\Avatar\Avatar($config);
$avatar2->setGenerator($generator);
$avatar2->create($name2)->buildAvatar();

$this->assertEquals('#000000', $avatar1->getAttribute('background'));
Expand Down
16 changes: 0 additions & 16 deletions tests/AvatarPhpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,6 @@ public function it_can_generate_base64()
$this->assertEquals($expected, $result);
}

/**
* @test
*/
public function it_can_generate_base64_from_cache()
{
$cachedAvatar = 'data:image/png;base64,iVBO';

$cache = Mockery::mock('Illuminate\Contracts\Cache\Repository');
$cache->shouldReceive('get')->andReturn($cachedAvatar);

$avatar = new \Laravolt\Avatar\Avatar([], $cache);
$result = (string)$avatar->create('Citra')->setDimension(5, 5)->toBase64();

$this->assertEquals($cachedAvatar, $result);
}

/**
* @test
*/
Expand Down