Lines 100.00% 27 / 27
Functions and Methods 100.00% 8 / 8
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Slot 100.00% 27 / 27 100.00% 8 / 8 15 100.00% 1 / 1
 getDefaults 100.00% 5 / 5 100.00% 1 / 1 1
 setTime 100.00% 1 / 1 100.00% 1 / 1 1
 hasTime 100.00% 1 / 1 100.00% 1 / 1 2
 getTimeString 100.00% 5 / 5 100.00% 1 / 1 3
 removeAppointment 100.00% 6 / 6 100.00% 1 / 1 3
 withAddedSlot 100.00% 5 / 5 100.00% 1 / 1 3
 __toString 100.00% 3 / 3 100.00% 1 / 1 1
 __clone 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3namespace BO\Zmsentities;
4
5class Slot extends Schema\Entity
6{
7    public static $schema = "slot.json";
8
9    /**
10     *  the values represent possible free appointments without confirmed
11     *  appointments
12     *
13     */
14    public const string FREE = 'free';
15
16    /**
17     *  the values represent free appointments for a given day. Confirmed and
18     *  reserved appointments on processes are substracted.
19     */
20    public const string TIMESLICE = 'timeslice';
21
22    /**
23     * like timeslice, but for more than one scope
24     */
25    public const string SUM = 'sum';
26
27    /**
28     * like timeslice, but numbers were reduced due to required slots on a
29     * given request
30     *
31     */
32    public const string REDUCED = 'reduced';
33
34    /**
35     * the values represent a unix timestamp to when there are free processes
36     *
37     */
38    public const string TIMESTAMP = 'timestamp';
39
40    /**
41     * @return (int|string)[]
42     *
43     */
44    #[\Override]
45    public function getDefaults()
46    {
47        return [
48            'public' => 0,
49            'intern' => 0,
50            'type' => self::FREE,
51        ];
52    }
53
54    public function setTime(Helper\DateTime $slotTime): void
55    {
56        $this->time = $slotTime->format('H:i');
57    }
58
59    public function hasTime(): bool
60    {
61        return ($this->toProperty()->time->get()) ? true : false;
62    }
63
64    public function getTimeString(): mixed
65    {
66        if (null === $this->toProperty()->time->get()) {
67            return '0:00';
68        }
69        if ($this->toProperty()->time->get() instanceof \DateTimeInterface) {
70            return $this['time']->format('H:i');
71        }
72        return $this->toProperty()->time->get();
73    }
74
75    public function removeAppointment(): static
76    {
77        if ($this->intern <= 0) {
78            throw new Exception\SlotFull("Could not remove another appointment from $this");
79        }
80        $this->intern = $this->intern - 1;
81        if ($this->public > 0) {
82            $this->public = $this->public - 1;
83        }
84        return $this;
85    }
86
87    public function withAddedSlot(Slot $slot): self
88    {
89        $slot = clone $slot;
90        $slot->type = 'sum';
91        $slot->intern = (($slot->intern > 0) ? $slot->intern : 0) + (($this->intern > 0) ? $this->intern : 0);
92        $slot->public = $slot->public + $this->public;
93        return $slot;
94    }
95
96    public function __toString()
97    {
98        return "slot#{$this->type}@"
99            . "{$this->getTimeString()}"
100            . " p/i={$this->public}/{$this->intern}";
101    }
102
103    /**
104     * Keep empty, no sub-instances
105     * ATTENTION: Keep highly optimized, time critical function
106     */
107    #[\Override]
108    public function __clone()
109    {
110    }
111}