#20 - wip
This commit is contained in:
16
app/Helpers/Rules/ApprovedVacationDaysInSameRange.php
Normal file
16
app/Helpers/Rules/ApprovedVacationDaysInSameRange.php
Normal 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);
|
||||
}
|
||||
}
|
16
app/Helpers/Rules/DoesNotExceedLimitRule.php
Normal file
16
app/Helpers/Rules/DoesNotExceedLimitRule.php
Normal 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);
|
||||
}
|
||||
}
|
16
app/Helpers/Rules/MinimumOneVacationDayRule.php
Normal file
16
app/Helpers/Rules/MinimumOneVacationDayRule.php
Normal 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);
|
||||
}
|
||||
}
|
16
app/Helpers/Rules/PendingVacationRequestInSameRange.php
Normal file
16
app/Helpers/Rules/PendingVacationRequestInSameRange.php
Normal 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);
|
||||
}
|
||||
}
|
13
app/Helpers/Rules/VacationRequestRule.php
Normal file
13
app/Helpers/Rules/VacationRequestRule.php
Normal 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);
|
||||
}
|
76
app/Helpers/VacationRequestStateManager.php
Normal file
76
app/Helpers/VacationRequestStateManager.php
Normal 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);
|
||||
}
|
||||
}
|
35
app/Helpers/VacationRequestValidator.php
Normal file
35
app/Helpers/VacationRequestValidator.php
Normal 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");
|
||||
}
|
||||
}
|
46
app/Helpers/VacationTypeConfigRetriever.php
Normal file
46
app/Helpers/VacationTypeConfigRetriever.php
Normal 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}");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user