engelsystem/includes/model/ShiftSignupState.php

90 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Engelsystem;
use Engelsystem\Models\Shifts\ShiftSignupStatus;
/**
* BO to represent if there are free slots on a shift for a given angeltype
* and if signup for a given user is possible (or not, because of collisions, etc.)
*/
2017-01-02 03:57:23 +01:00
class ShiftSignupState
{
2017-01-03 03:22:48 +01:00
/** @var int */
2017-01-02 03:57:23 +01:00
private $freeEntries;
2017-01-03 03:22:48 +01:00
/**
* ShiftSignupState constructor.
*
* @param ShiftSignupStatus $state
* @param int $free_entries
2017-01-03 03:22:48 +01:00
*/
public function __construct(private ShiftSignupStatus $state, $free_entries)
2017-01-02 03:57:23 +01:00
{
$this->freeEntries = $free_entries;
}
2017-01-02 15:43:36 +01:00
/**
* Combine this state with another state from the same shift.
*
2017-12-25 23:12:52 +01:00
* @param ShiftSignupState $shiftSignupState The other state to combine
2017-01-02 15:43:36 +01:00
*/
public function combineWith(ShiftSignupState $shiftSignupState)
{
$this->freeEntries += $shiftSignupState->getFreeEntries();
if ($this->valueForState($shiftSignupState->state) > $this->valueForState($this->state)) {
$this->state = $shiftSignupState->state;
}
}
2016-12-29 15:24:57 +01:00
2017-01-03 03:22:48 +01:00
/**
* @param ShiftSignupStatus $state
2017-01-03 03:22:48 +01:00
* @return int
*/
private function valueForState(ShiftSignupStatus $state)
2017-01-02 03:57:23 +01:00
{
2022-12-22 00:08:54 +01:00
return match ($state) {
ShiftSignupStatus::NOT_ARRIVED, ShiftSignupStatus::NOT_YET, ShiftSignupStatus::SHIFT_ENDED => 100,
ShiftSignupStatus::SIGNED_UP => 90,
ShiftSignupStatus::FREE => 80,
ShiftSignupStatus::ANGELTYPE, ShiftSignupStatus::COLLIDES => 70,
ShiftSignupStatus::OCCUPIED, ShiftSignupStatus::ADMIN => 60,
2022-12-22 00:08:54 +01:00
default => 0,
};
}
2017-01-02 15:43:36 +01:00
/**
* Returns true, if signup is allowed
2017-01-03 03:22:48 +01:00
*
* @return bool
2017-01-02 15:43:36 +01:00
*/
public function isSignupAllowed()
{
2022-12-22 00:08:54 +01:00
return match ($this->state) {
ShiftSignupStatus::FREE, ShiftSignupStatus::ADMIN => true,
2022-12-22 00:08:54 +01:00
default => false,
};
2017-01-02 15:43:36 +01:00
}
/**
* Return the shift signup state
2017-01-03 03:22:48 +01:00
*
* @return ShiftSignupStatus
2017-01-02 15:43:36 +01:00
*/
public function getState(): ShiftSignupStatus
2017-01-02 15:43:36 +01:00
{
return $this->state;
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
/**
* How many places are free in this shift for the angeltype?
2017-01-03 03:22:48 +01:00
*
* @return int
2017-01-02 15:43:36 +01:00
*/
public function getFreeEntries()
{
return $this->freeEntries;
}
}