#4 - wip
This commit is contained in:
parent
13d166ee81
commit
55eb288e23
49
app/Helpers/UserAvatarGenerator.php
Normal file
49
app/Helpers/UserAvatarGenerator.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Helpers;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
|
||||
use SVG\SVG;
|
||||
use Toby\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->getRandomColor())
|
||||
->color("#F4F8FD")
|
||||
->smooth()
|
||||
->generateSvg($user->name);
|
||||
}
|
||||
|
||||
protected function getRandomColor(): string
|
||||
{
|
||||
$colors = config("colors");
|
||||
|
||||
return Arr::random($colors);
|
||||
}
|
||||
|
||||
protected function generateUuid(): string
|
||||
{
|
||||
return Str::uuid()->toString();
|
||||
}
|
||||
}
|
@ -4,21 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace Toby\Observers;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use LasseRafn\InitialAvatarGenerator\InitialAvatar;
|
||||
use Toby\Helpers\UserAvatarGenerator;
|
||||
use Toby\Models\User;
|
||||
|
||||
class UserObserver
|
||||
{
|
||||
public function __construct(
|
||||
protected InitialAvatar $generator,
|
||||
protected UserAvatarGenerator $generator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function created(User $user): void
|
||||
{
|
||||
$user->avatar = $this->generateAvatar($user);
|
||||
$user->avatar = $this->generator->generateFor($user);
|
||||
|
||||
$user->save();
|
||||
}
|
||||
@ -26,7 +25,8 @@ class UserObserver
|
||||
public function updating(User $user): void
|
||||
{
|
||||
if ($user->isDirty("name")) {
|
||||
$user->avatar = $this->generateAvatar($user);
|
||||
Storage::delete($user->avatar);
|
||||
$user->avatar = $this->generator->generateFor($user);
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,20 +34,4 @@ class UserObserver
|
||||
{
|
||||
Storage::delete($user->avatar);
|
||||
}
|
||||
|
||||
protected function generateAvatar(User $user): string
|
||||
{
|
||||
$path = "avatars/{$user->id}.svg";
|
||||
|
||||
Storage::put($path, $this->generator->rounded()->background($this->getRandomColor())->color("#F4F8FD")->smooth()->generateSvg($user->name));
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
protected function getRandomColor(): string
|
||||
{
|
||||
$colors = config("colors");
|
||||
|
||||
return Arr::random($colors);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user