#48 - user avatars using ui-avatars.com

This commit is contained in:
Adrian Hopek
2022-03-04 09:25:28 +01:00
parent 697f3395bd
commit ecae457c1f
21 changed files with 192 additions and 266 deletions

View File

@@ -1,49 +0,0 @@
<?php
declare(strict_types=1);
namespace Toby\Eloquent\Helpers;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
use SVG\SVG;
use Toby\Eloquent\Models\User;
class UserAvatarGenerator
{
public function __construct(
protected InitialAvatar $generator,
) {}
public function generateFor(User $user): string
{
$path = "avatars/{$this->generateUuid()}.svg";
Storage::put($path, $this->generate($user));
return $path;
}
protected function generate(User $user): SVG
{
return $this->generator->rounded()
->background($this->getColor($user->fullName))
->color("#F4F8FD")
->smooth()
->fontSize(0.33)
->generateSvg($user->fullName);
}
protected function getColor(string $name): string
{
$colors = config("colors");
return $colors[strlen($name) % count($colors)];
}
protected function generateUuid(): string
{
return Str::uuid()->toString();
}
}

View File

@@ -13,6 +13,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Rackbeat\UIAvatars\HasAvatar;
use Toby\Domain\Enums\EmploymentForm;
use Toby\Domain\Enums\Role;
@@ -35,6 +36,7 @@ class User extends Authenticatable
use HasFactory;
use Notifiable;
use SoftDeletes;
use HasAvatar;
protected $guarded = [];
@@ -80,11 +82,13 @@ class User extends Authenticatable
->orWhere("email", "ILIKE", $text);
}
public function saveAvatar(string $path): void
public function getAvatar(): string
{
$this->avatar = $path;
$colors = config("colors");
$this->save();
return $this->getAvatarGenerator()
->backgroundColor($colors[strlen($this->fullname) % count($colors)])
->image();
}
public function getFullNameAttribute(): string
@@ -97,6 +101,11 @@ class User extends Authenticatable
return $this->role === $role;
}
protected function getAvatarNameKey(): string
{
return "fullName";
}
protected static function newFactory(): UserFactory
{
return UserFactory::new();

View File

@@ -4,37 +4,19 @@ declare(strict_types=1);
namespace Toby\Eloquent\Observers;
use Illuminate\Support\Facades\Storage;
use Toby\Eloquent\Helpers\UserAvatarGenerator;
use Toby\Eloquent\Helpers\YearPeriodRetriever;
use Toby\Eloquent\Models\User;
class UserObserver
{
public function __construct(
protected UserAvatarGenerator $generator,
protected YearPeriodRetriever $yearPeriodRetriever,
) {}
public function created(User $user): void
{
$user->saveAvatar($this->generator->generateFor($user));
$user->vacationLimits()->create([
"year_period_id" => $this->yearPeriodRetriever->current()->id,
]);
}
public function updating(User $user): void
{
if ($user->isDirty(["first_name", "last_name"])) {
Storage::delete($user->avatar);
$user->avatar = $this->generator->generateFor($user);
}
}
public function forceDeleted(User $user): void
{
Storage::delete($user->avatar);
}
}

View File

@@ -5,14 +5,12 @@ declare(strict_types=1);
namespace Toby\Eloquent\Observers;
use Toby\Domain\PolishHolidaysRetriever;
use Toby\Eloquent\Helpers\UserAvatarGenerator;
use Toby\Eloquent\Models\User;
use Toby\Eloquent\Models\YearPeriod;
class YearPeriodObserver
{
public function __construct(
protected UserAvatarGenerator $generator,
protected PolishHolidaysRetriever $polishHolidaysRetriever,
) {}