engelsystem/includes/model/ShiftSignupState.php

155 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Engelsystem;
/**
* 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-02 15:43:36 +01:00
/**
* Shift has free places
*/
public const FREE = 'FREE';
2017-01-02 15:43:36 +01:00
/**
* Shift collides with users shifts
*/
public const COLLIDES = 'COLLIDES';
2017-01-02 15:43:36 +01:00
/**
* User cannot join because of a restricted angeltype or user is not in the angeltype
*/
public const ANGELTYPE = 'ANGELTYPE';
2017-01-02 15:43:36 +01:00
/**
* Shift is full
*/
public const OCCUPIED = 'OCCUPIED';
2017-01-02 15:43:36 +01:00
/**
* User is admin and can do what he wants.
*/
public const ADMIN = 'ADMIN';
2017-01-02 15:43:36 +01:00
/**
* Shift has already ended, no signup
*/
public const SHIFT_ENDED = 'SHIFT_ENDED';
2017-01-02 15:43:36 +01:00
/**
* Shift is not available yet
*/
public const NOT_YET = 'NOT_YET';
2017-01-02 15:43:36 +01:00
/**
* User is already signed up
*/
public const SIGNED_UP = 'SIGNED_UP';
2017-12-25 23:12:52 +01:00
/**
* User has to be arrived
*/
public const NOT_ARRIVED = 'NOT_ARRIVED';
2017-12-25 23:12:52 +01:00
2017-01-03 03:22:48 +01:00
/** @var string */
2017-01-02 03:57:23 +01:00
private $state;
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 string $state
* @param int $free_entries
*/
2017-01-02 03:57:23 +01:00
public function __construct($state, $free_entries)
{
$this->state = $state;
$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 string $state
* @return int
*/
2017-01-02 03:57:23 +01:00
private function valueForState($state)
{
switch ($state) {
case ShiftSignupState::NOT_ARRIVED:
2019-06-04 19:26:50 +02:00
case ShiftSignupState::NOT_YET:
2017-01-02 15:43:36 +01:00
case ShiftSignupState::SHIFT_ENDED:
return 100;
case ShiftSignupState::SIGNED_UP:
return 90;
case ShiftSignupState::FREE:
return 80;
case ShiftSignupState::ANGELTYPE:
case ShiftSignupState::COLLIDES:
return 70;
case ShiftSignupState::OCCUPIED:
case ShiftSignupState::ADMIN:
return 60;
2019-06-04 19:26:50 +02:00
2017-01-03 03:22:48 +01:00
default:
return 0;
2017-01-02 15:43:36 +01:00
}
}
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()
{
switch ($this->state) {
case ShiftSignupState::FREE:
case ShiftSignupState::ADMIN:
return true;
}
2017-12-25 23:12:52 +01:00
2017-01-02 15:43:36 +01:00
return false;
}
/**
* Return the shift signup state
2017-01-03 03:22:48 +01:00
*
* @return string
2017-01-02 15:43:36 +01:00
*/
public function getState()
{
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;
}
}