Schedule.php 3.56 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Source\Entities;

use Carbon\Carbon;
7
use InvalidArgumentException;
Jacob Priddy's avatar
Jacob Priddy committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class Schedule
{
    public const TYPE_OPEN_MODE = 0;
    public const TYPE_USER_ACCESS = 1;

    protected int $id;

    protected int $groupId;

    protected int $type;

    protected string $rset;

Jacob Priddy's avatar
Jacob Priddy committed
22
    protected int $duration;
Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

    /**
     * @var \Carbon\Carbon
     */
    protected Carbon $start;

    /**
     * @var \Carbon\Carbon|null
     */
    protected ?Carbon $end;

    /**
     * @var \Carbon\Carbon|null
     */
    protected ?Carbon $createdAt;

    /**
     * @var \Carbon\Carbon|null
     */
    protected ?Carbon $updatedAt;

44
45
46
47
48
    /**
     * @var string
     */
    protected string $description;

Jacob Priddy's avatar
Jacob Priddy committed
49
50
51
52
53
    public function __construct(
        int $id,
        int $groupId,
        int $type,
        string $rset,
Jacob Priddy's avatar
Jacob Priddy committed
54
        int $duration,
55
        string $description,
Jacob Priddy's avatar
Jacob Priddy committed
56
        Carbon $start,
57
58
59
        ?Carbon $end = null,
        ?Carbon $createdAt = null,
        ?Carbon $updatedAt = null
Jacob Priddy's avatar
Jacob Priddy committed
60
    ) {
61
        if ($type > self::TYPE_USER_ACCESS || $type < self::TYPE_OPEN_MODE) {
62
            throw new InvalidArgumentException('Type not valid schedule type.');
63
        }
Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
67
68
69
70
71
72
        $this->id = $id;
        $this->groupId = $groupId;
        $this->type = $type;
        $this->rset = $rset;
        $this->duration = $duration;
        $this->start = $start;
        $this->end = $end;
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
73
        $this->description = $description;
Jacob Priddy's avatar
Jacob Priddy committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    }

    /**
     * @return int
     */
    public function getId(): int
    {
        return $this->id;
    }

    /**
     * @return int
     */
    public function getGroupId(): int
    {
        return $this->groupId;
    }

    /**
93
     * @return int
Jacob Priddy's avatar
Jacob Priddy committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
     */
    public function getType(): int
    {
        return $this->type;
    }

    /**
     * @return string
     */
    public function getRset(): string
    {
        return $this->rset;
    }

    /**
Jacob Priddy's avatar
Jacob Priddy committed
109
110
111
     * Currently this duration is in milliseconds
     *
     * @return int
Jacob Priddy's avatar
Jacob Priddy committed
112
     */
Jacob Priddy's avatar
Jacob Priddy committed
113
    public function getDuration(): int
Jacob Priddy's avatar
Jacob Priddy committed
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
148
    {
        return $this->duration;
    }

    /**
     * @return \Carbon\Carbon
     */
    public function getStart(): Carbon
    {
        return $this->start;
    }

    /**
     * @return \Carbon\Carbon|null
     */
    public function getEnd(): ?Carbon
    {
        return $this->end;
    }

    /**
     * @return \Carbon\Carbon|null
     */
    public function getCreatedAt(): ?Carbon
    {
        return $this->createdAt;
    }

    /**
     * @return \Carbon\Carbon|null
     */
    public function getUpdatedAt(): ?Carbon
    {
        return $this->updatedAt;
    }
Jacob Priddy's avatar
Jacob Priddy committed
149
150
151
152
153
154
155

    /**
     * @param string $groupId
     * @return bool
     */
    public function hasGroupIdOf(?string $groupId): bool
    {
Jacob Priddy's avatar
Jacob Priddy committed
156
        return $this->groupId === (int)$groupId;
Jacob Priddy's avatar
Jacob Priddy committed
157
158
159
160
161
162
163
164
    }

    /**
     * @param string $scheduleId
     * @return bool
     */
    public function hasIdOf(?string $scheduleId): bool
    {
Jacob Priddy's avatar
Jacob Priddy committed
165
        return $this->id === (int)$scheduleId;
Jacob Priddy's avatar
Jacob Priddy committed
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    }

    /**
     * @param \Carbon\Carbon $date
     * @return bool
     */
    public function isActiveForDate(Carbon $date): bool
    {
        if ($this->getEnd()) {
            return $date->isBetween($this->getStart(), $this->getEnd());
        }

        return $date->isAfter($this->getStart());
    }
180

181
182
183
184
    /**
     * @param int|null $type
     * @return bool
     */
185
186
187
188
    public function hasTypeOf(?int $type): bool
    {
        return $this->type === $type;
    }
189
190
191
192
193
194
195
196

    /**
     * @return string
     */
    public function getDescription(): string
    {
        return $this->description;
    }
Jacob Priddy's avatar
Jacob Priddy committed
197
}