engelsystem/includes/model/ShiftSignupState.php

123 lines
2.4 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
{
/**
* Shift has free places
*/
const FREE = 'FREE';
/**
* Shift collides with users shifts
*/
const COLLIDES = 'COLLIDES';
/**
* User cannot join because of a restricted angeltype or user is not in the angeltype
*/
const ANGELTYPE = 'ANGELTYPE';
/**
* Shift is full
*/
const OCCUPIED = 'OCCUPIED';
/**
* User is admin and can do what he wants.
*/
const ADMIN = 'ADMIN';
/**
* Shift has already ended, no signup
*/
const SHIFT_ENDED = 'SHIFT_ENDED';
/**
* User is already signed up
*/
const SIGNED_UP = 'SIGNED_UP';
2017-01-02 03:57:23 +01:00
private $state;
2017-01-02 03:57:23 +01:00
private $freeEntries;
2017-01-02 03:57:23 +01:00
public function __construct($state, $free_entries)
{
$this->state = $state;
$this->freeEntries = $free_entries;
}
/**
* Combine this state with another state from the same shift.
*
* @param ShiftSignupState $shiftSignupState
* The other state to combine
*/
2017-01-02 03:57:23 +01:00
public function combineWith(ShiftSignupState $shiftSignupState)
{
$this->freeEntries += $shiftSignupState->getFreeEntries();
2017-01-02 03:57:23 +01:00
if ($this->valueForState($shiftSignupState->state) > $this->valueForState($this->state)) {
$this->state = $shiftSignupState->state;
}
2016-12-29 15:24:57 +01:00
}
2017-01-02 03:57:23 +01:00
private function valueForState($state)
{
switch ($state) {
2016-12-29 15:24:57 +01:00
case ShiftSignupState::SHIFT_ENDED:
return 100;
case ShiftSignupState::SIGNED_UP:
return 90;
case ShiftSignupState::FREE:
return 80;
case ShiftSignupState::ANGELTYPE:
2016-12-29 15:24:57 +01:00
case ShiftSignupState::COLLIDES:
return 70;
2016-12-23 12:48:34 +01:00
case ShiftSignupState::OCCUPIED:
2016-12-29 15:24:57 +01:00
case ShiftSignupState::ADMIN:
return 60;
}
2017-01-02 03:57:23 +01:00
}
/**
* Returns true, if signup is allowed
*/
2017-01-02 03:57:23 +01:00
public function isSignupAllowed()
{
switch ($this->state) {
case ShiftSignupState::FREE:
case ShiftSignupState::ADMIN:
return true;
}
2017-01-02 03:57:23 +01:00
return false;
}
/**
* Return the shift signup state
*/
2017-01-02 03:57:23 +01:00
public function getState()
{
return $this->state;
}
/**
* How many places are free in this shift for the angeltype?
*/
2017-01-02 03:57:23 +01:00
public function getFreeEntries()
{
return $this->freeEntries;
}
}