summaryrefslogtreecommitdiff
path: root/includes/model/ShiftSignupState.php
blob: 8d588de3dcce382ae85791bf7923d27eed9b4b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?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.)
 */
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';

    /**
     * User has to be arrived
     */
    const NOT_ARRIVED = 'NOT_ARRIVED';

    /** @var string */
    private $state;

    /** @var int */
    private $freeEntries;

    /**
     * ShiftSignupState constructor.
     *
     * @param string $state
     * @param int    $free_entries
     */
    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
     */
    public function combineWith(ShiftSignupState $shiftSignupState)
    {
        $this->freeEntries += $shiftSignupState->getFreeEntries();

        if ($this->valueForState($shiftSignupState->state) > $this->valueForState($this->state)) {
            $this->state = $shiftSignupState->state;
        }
    }

    /**
     * @param string $state
     * @return int
     */
    private function valueForState($state)
    {
        switch ($state) {
            case ShiftSignupState::NOT_ARRIVED:
            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;
            default:
                return 0;
        }
    }

    /**
     * Returns true, if signup is allowed
     *
     * @return bool
     */
    public function isSignupAllowed()
    {
        switch ($this->state) {
            case ShiftSignupState::FREE:
            case ShiftSignupState::ADMIN:
                return true;
        }

        return false;
    }

    /**
     * Return the shift signup state
     *
     * @return string
     */
    public function getState()
    {
        return $this->state;
    }

    /**
     * How many places are free in this shift for the angeltype?
     *
     * @return int
     */
    public function getFreeEntries()
    {
        return $this->freeEntries;
    }
}