- 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),
|
||||
];
|
||||
}
|
||||
}
|
38
app/Eloquent/Models/Holiday.php
Normal file
38
app/Eloquent/Models/Holiday.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Models;
|
||||
|
||||
use Database\Factories\HolidayFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property Carbon $date
|
||||
* @property YearPeriod $yearPeriod
|
||||
*/
|
||||
class Holiday extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
"date" => "date",
|
||||
];
|
||||
|
||||
public function yearPeriod(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(YearPeriod::class);
|
||||
}
|
||||
|
||||
protected static function newFactory(): HolidayFactory
|
||||
{
|
||||
return HolidayFactory::new();
|
||||
}
|
||||
}
|
78
app/Eloquent/Models/User.php
Normal file
78
app/Eloquent/Models/User.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Models;
|
||||
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Toby\Domain\EmploymentForm;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $first_name
|
||||
* @property string $last_name
|
||||
* @property string $email
|
||||
* @property string $avatar
|
||||
* @property EmploymentForm $employment_form
|
||||
* @property Carbon $employment_date
|
||||
* @property Collection $vacationLimits
|
||||
*/
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory;
|
||||
use Notifiable;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
protected $casts = [
|
||||
"employment_form" => EmploymentForm::class,
|
||||
"employment_date" => "date",
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
"remember_token",
|
||||
];
|
||||
|
||||
public function vacationLimits(): HasMany
|
||||
{
|
||||
return $this->hasMany(VacationLimit::class);
|
||||
}
|
||||
|
||||
public function scopeSearch(Builder $query, ?string $text): Builder
|
||||
{
|
||||
if ($text === null) {
|
||||
return $query;
|
||||
}
|
||||
|
||||
return $query
|
||||
->where("first_name", "LIKE", "%{$text}%")
|
||||
->orWhere("last_name", "LIKE", "%{$text}%")
|
||||
->orWhere("email", "LIKE", "%{$text}%");
|
||||
}
|
||||
|
||||
public function saveAvatar(string $path): void
|
||||
{
|
||||
$this->avatar = $path;
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function getFullNameAttribute(): string
|
||||
{
|
||||
return "{$this->first_name} {$this->last_name}";
|
||||
}
|
||||
|
||||
protected static function newFactory(): UserFactory
|
||||
{
|
||||
return UserFactory::new();
|
||||
}
|
||||
}
|
51
app/Eloquent/Models/VacationLimit.php
Normal file
51
app/Eloquent/Models/VacationLimit.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Models;
|
||||
|
||||
use Database\Factories\VacationLimitFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property User $user
|
||||
* @property YearPeriod $yearPeriod
|
||||
* @property int $days
|
||||
*/
|
||||
class VacationLimit extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public function hasVacation(): bool
|
||||
{
|
||||
return $this->days !== null;
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function yearPeriod(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(YearPeriod::class);
|
||||
}
|
||||
|
||||
public function scopeOrderByUserField(Builder $query, string $field): Builder
|
||||
{
|
||||
$userQuery = User::query()->select($field)->whereColumn("vacation_limits.user_id", "users.id");
|
||||
|
||||
return $query->orderBy($userQuery);
|
||||
}
|
||||
|
||||
protected static function newFactory(): VacationLimitFactory
|
||||
{
|
||||
return VacationLimitFactory::new();
|
||||
}
|
||||
}
|
53
app/Eloquent/Models/YearPeriod.php
Normal file
53
app/Eloquent/Models/YearPeriod.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Models;
|
||||
|
||||
use Database\Factories\YearPeriodFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $year
|
||||
* @property Collection $vacationLimits
|
||||
* @property Collection $holidays
|
||||
*/
|
||||
class YearPeriod extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public static function current(): ?static
|
||||
{
|
||||
return static::findByYear(Carbon::now()->year);
|
||||
}
|
||||
|
||||
public static function findByYear(int $year): ?static
|
||||
{
|
||||
/** @var YearPeriod $year */
|
||||
$year = static::query()->where("year", $year)->first();
|
||||
|
||||
return $year;
|
||||
}
|
||||
|
||||
public function vacationLimits(): HasMany
|
||||
{
|
||||
return $this->hasMany(VacationLimit::class);
|
||||
}
|
||||
|
||||
public function holidays(): HasMany
|
||||
{
|
||||
return $this->hasMany(Holiday::class);
|
||||
}
|
||||
|
||||
protected static function newFactory(): YearPeriodFactory
|
||||
{
|
||||
return YearPeriodFactory::new();
|
||||
}
|
||||
}
|
41
app/Eloquent/Observers/UserObserver.php
Normal file
41
app/Eloquent/Observers/UserObserver.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
48
app/Eloquent/Observers/YearPeriodObserver.php
Normal file
48
app/Eloquent/Observers/YearPeriodObserver.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
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,
|
||||
) {
|
||||
}
|
||||
|
||||
public function created(YearPeriod $yearPeriod): void
|
||||
{
|
||||
$this->createVacationLimitsFor($yearPeriod);
|
||||
$this->createHolidaysFor($yearPeriod);
|
||||
}
|
||||
|
||||
protected function createVacationLimitsFor(YearPeriod $yearPeriod): void
|
||||
{
|
||||
$users = User::all();
|
||||
|
||||
foreach ($users as $user) {
|
||||
$yearPeriod->vacationLimits()->create([
|
||||
"user_id" => $user->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createHolidaysFor(YearPeriod $yearPeriod): void
|
||||
{
|
||||
$holidays = $this->polishHolidaysRetriever->getForYearPeriod($yearPeriod);
|
||||
|
||||
foreach ($holidays as $holiday) {
|
||||
$yearPeriod->holidays()->create([
|
||||
"name" => $holiday["name"],
|
||||
"date" => $holiday["date"],
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
23
app/Eloquent/Scopes/SelectedYearPeriodScope.php
Normal file
23
app/Eloquent/Scopes/SelectedYearPeriodScope.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Toby\Eloquent\Scopes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Scope;
|
||||
use Toby\Eloquent\Helpers\YearPeriodRetriever;
|
||||
|
||||
class SelectedYearPeriodScope implements Scope
|
||||
{
|
||||
public function __construct(
|
||||
protected YearPeriodRetriever $yearPeriodRetriever,
|
||||
) {
|
||||
}
|
||||
|
||||
public function apply(Builder $builder, Model $model): Builder
|
||||
{
|
||||
return $builder->where("year_period_id", $this->yearPeriodRetriever->selected()->id);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user