This commit is contained in:
Adrian Hopek
2022-01-26 10:56:25 +01:00
parent 026bfe485f
commit 01de63e9f4
71 changed files with 5631 additions and 3587 deletions

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers\Rules;
use Closure;
use Toby\Models\VacationRequest;
class ApprovedVacationDaysInSameRange implements VacationRequestRule
{
public function check(VacationRequest $vacationRequest, Closure $next)
{
return $next($vacationRequest);
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers\Rules;
use Closure;
use Toby\Models\VacationRequest;
class DoesNotExceedLimitRule implements VacationRequestRule
{
public function check(VacationRequest $vacationRequest, Closure $next)
{
return $next($vacationRequest);
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers\Rules;
use Closure;
use Toby\Models\VacationRequest;
class MinimumOneVacationDayRule implements VacationRequestRule
{
public function check(VacationRequest $vacationRequest, Closure $next)
{
return $next($vacationRequest);
}
}

View File

@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers\Rules;
use Closure;
use Toby\Models\VacationRequest;
class PendingVacationRequestInSameRange implements VacationRequestRule
{
public function check(VacationRequest $vacationRequest, Closure $next)
{
return $next($vacationRequest);
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers\Rules;
use Closure;
use Toby\Models\VacationRequest;
interface VacationRequestRule
{
public function check(VacationRequest $vacationRequest, Closure $next);
}

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers;
use Illuminate\Contracts\Auth\Factory as Auth;
use Illuminate\Contracts\Events\Dispatcher;
use Toby\Enums\VacationRequestState;
use Toby\Events\VacationRequestAcceptedByAdministrative;
use Toby\Events\VacationRequestAcceptedByTechnical;
use Toby\Events\VacationRequestApproved;
use Toby\Events\VacationRequestCreated;
use Toby\Models\VacationRequest;
class VacationRequestStateManager
{
public function __construct(
protected Auth $auth,
protected Dispatcher $dispatcher,
) {
}
public function markAsCreated(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::CREATED);
$this->dispatcher->dispatch(new VacationRequestCreated($vacationRequest));
}
public function approve(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::APPROVED);
$this->dispatcher->dispatch(new VacationRequestApproved($vacationRequest));
}
public function reject(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::REJECTED);
}
public function cancel(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::CANCELED);
}
public function acceptAsTechnical(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::ACCEPTED_BY_TECHNICAL);
$this->dispatcher->dispatch(new VacationRequestAcceptedByTechnical($vacationRequest));
}
public function acceptAsAdministrative(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::ACCEPTED_BY_ADMINSTRATIVE);
$this->dispatcher->dispatch(new VacationRequestAcceptedByAdministrative($vacationRequest));
}
public function waitForTechnical(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::WAITING_FOR_TECHNICAL);
}
public function waitForAdministrative(VacationRequest $vacationRequest): void
{
$this->changeState($vacationRequest, VacationRequestState::WAITING_FOR_ADMINISTRATIVE);
}
protected function changeState(VacationRequest $vacationRequest, VacationRequestState $state): void
{
$vacationRequest->changeStateTo($state);
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers;
use Illuminate\Pipeline\Pipeline;
use Toby\Helpers\Rules\ApprovedVacationDaysInSameRange;
use Toby\Helpers\Rules\DoesNotExceedLimitRule;
use Toby\Helpers\Rules\MinimumOneVacationDayRule;
use Toby\Helpers\Rules\PendingVacationRequestInSameRange;
use Toby\Models\VacationRequest;
class VacationRequestValidator
{
protected array $rules = [
MinimumOneVacationDayRule::class,
DoesNotExceedLimitRule::class,
PendingVacationRequestInSameRange::class,
ApprovedVacationDaysInSameRange::class,
];
public function __construct(
protected Pipeline $pipeline,
) {
}
public function validate(VacationRequest $vacationRequest): void
{
$this->pipeline
->send($vacationRequest)
->through($this->rules)
->via("check");
}
}

View File

@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace Toby\Helpers;
use Illuminate\Contracts\Config\Repository;
use Toby\Enums\VacationType;
class VacationTypeConfigRetriever
{
public const KEY_TECHNICAL_APPROVAL = "technical_approval";
public const KEY_ADMINISTRATIVE_APPROVAL = "administrative_approval";
public const KEY_BILLABLE = "billable";
public const KEY_HAS_LIMIT = "has_limit";
public function __construct(
protected Repository $config,
) {
}
public function needsTechnicalApproval(VacationType $type): bool
{
return $this->getConfigFor($type)[static::KEY_TECHNICAL_APPROVAL];
}
public function needsAdministrativeApproval(VacationType $type): bool
{
return $this->getConfigFor($type)[static::KEY_ADMINISTRATIVE_APPROVAL];
}
public function isBillable(VacationType $type): bool
{
return $this->getConfigFor($type)[static::KEY_BILLABLE];
}
public function hasLimit(VacationType $type): bool
{
return $this->getConfigFor($type)[static::KEY_HAS_LIMIT];
}
protected function getConfigFor(VacationType $type): array
{
return $this->config->get("vacation_types.{$type->value}");
}
}