Completed
Push — master ( 5cc4e6...c9be3a )
by Sam
07:56
created

Booking::getGuest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Model;
6
7
use Application\DBAL\Types\BookingStatusType;
8
use Application\Utility;
9
use Cake\Chronos\Chronos;
10
use Doctrine\Common\Collections\ArrayCollection;
11
use Doctrine\Common\Collections\Collection;
12
use Doctrine\ORM\Mapping as ORM;
13
use GraphQL\Doctrine\Annotation as API;
14
15
/**
16
 * A booking linking a user and several bookables
17
 *
18
 * @ORM\Entity(repositoryClass="Application\Repository\BookingRepository")
19
 * @ORM\AssociationOverrides({
20
 *     @ORM\AssociationOverride(name="owner", inversedBy="bookings")
21
 * })
22
 */
23
class Booking extends AbstractModel
24
{
25
    /**
26
     * @var string
27
     *
28
     * @ORM\Column(type="BookingStatus", length=10, options={"default" = BookingStatusType::APPLICATION})
29
     */
30
    private $status = BookingStatusType::APPLICATION;
31
32
    /**
33
     * @var int
34
     * @ORM\Column(type="integer", options={"unsigned" = true, "default" = 1})
35
     */
36
    private $participantCount = 1;
37
38
    /**
39
     * @var string
40
     *
41
     * @ORM\Column(type="string", length=50, options={"default" = ""})
42
     */
43
    private $destination = '';
44
45
    /**
46
     * @var string
47
     *
48
     * @ORM\Column(type="text", length=65535, options={"default" = ""})
49
     */
50
    private $startComment = '';
51
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(type="text", length=65535, options={"default" = ""})
56
     */
57
    private $endComment = '';
58
59
    /**
60
     * @var Chronos
61
     *
62
     * @ORM\Column(type="datetime")
63
     */
64
    private $startDate;
65
66
    /**
67
     * @var Chronos
68
     *
69
     * @ORM\Column(type="datetime", nullable=true)
70
     */
71
    private $endDate;
72
73
    /**
74
     * @var string
75
     *
76
     * @ORM\Column(type="string", length=50, options={"default" = ""})
77
     */
78
    private $estimatedEndDate = '';
79
80
    /**
81
     * @var Collection
82
     *
83
     * @ORM\ManyToMany(targetEntity="Bookable", inversedBy="bookings")
84
     */
85
    private $bookables;
86
87
    /**
88
     * Constructor
89
     */
90 13
    public function __construct()
91
    {
92 13
        $this->bookables = new ArrayCollection();
93 13
    }
94
95 9
    public function setOwner(User $owner = null): void
96
    {
97 9
        if ($this->getOwner()) {
98 4
            $this->getOwner()->bookingRemoved($this);
99
        }
100
101 9
        parent::setOwner($owner);
102
103 9
        if ($this->getOwner()) {
104 8
            $this->getOwner()->bookingAdded($this);
105
        }
106 9
    }
107
108
    /**
109
     * Total count of participant, at least 1.
110
     *
111
     * @return int
112
     */
113
    public function getParticipantCount(): int
114
    {
115
        return $this->participantCount;
116
    }
117
118
    /**
119
     * @param int $participantCount
120
     */
121
    public function setParticipantCount(int $participantCount): void
122
    {
123
        $this->participantCount = $participantCount;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getDestination(): string
130
    {
131
        return $this->destination;
132
    }
133
134
    /**
135
     * @param string $destination
136
     */
137
    public function setDestination(string $destination): void
138
    {
139
        $this->destination = $destination;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    public function getStartComment(): string
146
    {
147
        return $this->startComment;
148
    }
149
150
    /**
151
     * @param string $startComment
152
     */
153
    public function setStartComment(string $startComment): void
154
    {
155
        $this->startComment = $startComment;
156
    }
157
158
    /**
159
     * @return string
160
     */
161
    public function getEndComment(): string
162
    {
163
        return $this->endComment;
164
    }
165
166
    /**
167
     * @param string $endComment
168
     */
169
    public function setEndComment(string $endComment): void
170
    {
171
        $this->endComment = $endComment;
172
    }
173
174
    /**
175
     * @return Chronos
176
     */
177
    public function getStartDate(): Chronos
178
    {
179
        return $this->startDate;
180
    }
181
182
    /**
183
     * @param Chronos $startDate
184
     */
185 1
    public function setStartDate(Chronos $startDate): void
186
    {
187 1
        $this->startDate = $startDate;
188 1
    }
189
190
    /**
191
     * @return null|Chronos
192
     */
193
    public function getEndDate(): ?Chronos
194
    {
195
        return $this->endDate;
196
    }
197
198
    /**
199
     * @param null|Chronos $endDate
200
     */
201
    public function setEndDate(?Chronos $endDate): void
202
    {
203
        $this->endDate = $endDate;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getEstimatedEndDate(): string
210
    {
211
        return $this->estimatedEndDate;
212
    }
213
214
    /**
215
     * @param string $estimatedEndDate
216
     */
217
    public function setEstimatedEndDate(string $estimatedEndDate): void
218
    {
219
        $this->estimatedEndDate = $estimatedEndDate;
220
    }
221
222
    /**
223
     * @return Collection
224
     */
225 1
    public function getBookables(): Collection
226
    {
227 1
        return $this->bookables;
228
    }
229
230
    /**
231
     * Add bookable
232
     *
233
     * @param Bookable $bookable
234
     */
235 2
    public function addBookable(Bookable $bookable): void
236
    {
237 2
        if (!$this->bookables->contains($bookable)) {
238 2
            $this->bookables->add($bookable);
239 2
            $bookable->bookingAdded($this);
240
        }
241 2
    }
242
243
    /**
244
     * Remove bookable
245
     *
246
     * @param Bookable $bookable
247
     */
248 1
    public function removeBookable(Bookable $bookable): void
249
    {
250 1
        $this->bookables->removeElement($bookable);
251 1
        $bookable->bookingRemoved($this);
252 1
    }
253
254
    /**
255
     * @API\Field(type="BookingStatus")
256
     *
257
     * @return string
258
     */
259
    public function getStatus(): string
260
    {
261
        return $this->status;
262
    }
263
264
    /**
265
     * @API\Input(type="BookingStatus")
266
     *
267
     * @param string $status
268
     */
269 1
    public function setStatus(string $status): void
270
    {
271 1
        $this->status = $status;
272 1
    }
273
274
    /**
275
     * Mark the booking as terminated with an optional comment,
276
     * but only if not already terminated
277
     *
278
     * @param null|string $comment
279
     */
280
    public function terminate(?string $comment): void
281
    {
282
        // Booking can only be terminated once
283
        if (!$this->getEndDate()) {
284
            $this->setEndDate(Utility::getNow());
285
            if ($comment) {
286
                $this->setEndComment($comment);
287
            }
288
        }
289
    }
290
}
291