- directory refactor (#32)
* - directory refactor * - readme.md update * Update readme.md Co-authored-by: Adrian Hopek <adrian.hopek@blumilk.pl> * Update readme.md Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com> * Update readme.md Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com> * Update readme.md Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com> * Update readme.md Co-authored-by: Adrian Hopek <adrian.hopek@blumilk.pl> Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
This commit is contained in:
50
app/Eloquent/Helpers/UserAvatarGenerator.php
Normal file
50
app/Eloquent/Helpers/UserAvatarGenerator.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
57
app/Eloquent/Helpers/YearPeriodRetriever.php
Normal file
57
app/Eloquent/Helpers/YearPeriodRetriever.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Helpers;
|
||||
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Toby\Eloquent\Models\YearPeriod;
|
||||
|
||||
class YearPeriodRetriever
|
||||
{
|
||||
public const SESSION_KEY = "selected_year_period";
|
||||
|
||||
public function __construct(
|
||||
protected Session $session,
|
||||
) {
|
||||
}
|
||||
|
||||
public function selected(): YearPeriod
|
||||
{
|
||||
/** @var YearPeriod $yearPeriod */
|
||||
$yearPeriod = YearPeriod::query()->find($this->session->get(static::SESSION_KEY));
|
||||
|
||||
return $yearPeriod !== null ? $yearPeriod : $this->current();
|
||||
}
|
||||
|
||||
public function current(): YearPeriod
|
||||
{
|
||||
return YearPeriod::current();
|
||||
}
|
||||
|
||||
public function links(): array
|
||||
{
|
||||
$current = $this->selected();
|
||||
|
||||
$years = YearPeriod::query()->whereIn("year", $this->offset($current->year))->get();
|
||||
$navigation = $years->map(fn(YearPeriod $yearPeriod) => $this->toNavigation($yearPeriod));
|
||||
|
||||
return [
|
||||
"current" => $current->year,
|
||||
"navigation" => $navigation->toArray(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function offset(int $year): array
|
||||
{
|
||||
return range($year - 2, $year + 2);
|
||||
}
|
||||
|
||||
protected function toNavigation(YearPeriod $yearPeriod): array
|
||||
{
|
||||
return [
|
||||
"year" => $yearPeriod->year,
|
||||
"link" => route("year-periods.select", $yearPeriod->id),
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user