Failed Conditions
Push — master ( eb5eca...3e489f )
by Sam
06:57
created

Booking   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Test Coverage

Coverage 38.03%

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 289
ccs 27
cts 71
cp 0.3803
rs 10
c 0
b 0
f 0
wmc 29

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 3 1
A setEndDate() 0 3 1
A getEndDate() 0 3 1
A setEstimatedEndDate() 0 3 1
A addBookable() 0 5 2
A removeBookable() 0 4 1
A getStartDate() 0 3 1
A __construct() 0 3 1
A setOwner() 0 10 3
A getEndComment() 0 3 1
A getParticipantCount() 0 3 1
A terminate() 0 7 3
A setStatus() 0 3 1
A getGuest() 0 3 1
A setParticipantCount() 0 3 1
A setGuest() 0 3 1
A getBookables() 0 3 1
A setEndComment() 0 3 1
A getEstimatedEndDate() 0 3 1
A setStartComment() 0 3 1
A setDestination() 0 3 1
A getStartComment() 0 3 1
A getDestination() 0 3 1
A setStartDate() 0 3 1
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
     * @var bool
89
     *
90
     * @ORM\Column(type="boolean", options={"default" = 0})
91
     */
92
    private $guest = false;
93
94
    /**
95
     * Constructor
96
     */
97 13
    public function __construct()
98
    {
99 13
        $this->bookables = new ArrayCollection();
100 13
    }
101
102 9
    public function setOwner(User $owner = null): void
103
    {
104 9
        if ($this->getOwner()) {
105 4
            $this->getOwner()->bookingRemoved($this);
106
        }
107
108 9
        parent::setOwner($owner);
109
110 9
        if ($this->getOwner()) {
111 8
            $this->getOwner()->bookingAdded($this);
112
        }
113 9
    }
114
115
    /**
116
     * Total count of participant, at least 1.
117
     *
118
     * @return int
119
     */
120
    public function getParticipantCount(): int
121
    {
122
        return $this->participantCount;
123
    }
124
125
    /**
126
     * @param int $participantCount
127
     */
128
    public function setParticipantCount(int $participantCount): void
129
    {
130
        $this->participantCount = $participantCount;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getDestination(): string
137
    {
138
        return $this->destination;
139
    }
140
141
    /**
142
     * @param string $destination
143
     */
144
    public function setDestination(string $destination): void
145
    {
146
        $this->destination = $destination;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getStartComment(): string
153
    {
154
        return $this->startComment;
155
    }
156
157
    /**
158
     * @param string $startComment
159
     */
160
    public function setStartComment(string $startComment): void
161
    {
162
        $this->startComment = $startComment;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getEndComment(): string
169
    {
170
        return $this->endComment;
171
    }
172
173
    /**
174
     * @param string $endComment
175
     */
176
    public function setEndComment(string $endComment): void
177
    {
178
        $this->endComment = $endComment;
179
    }
180
181
    /**
182
     * @return Chronos
183
     */
184
    public function getStartDate(): Chronos
185
    {
186
        return $this->startDate;
187
    }
188
189
    /**
190
     * @param Chronos $startDate
191
     */
192 1
    public function setStartDate(Chronos $startDate): void
193
    {
194 1
        $this->startDate = $startDate;
195 1
    }
196
197
    /**
198
     * @return null|Chronos
199
     */
200
    public function getEndDate(): ?Chronos
201
    {
202
        return $this->endDate;
203
    }
204
205
    /**
206
     * @param null|Chronos $endDate
207
     */
208
    public function setEndDate(?Chronos $endDate): void
209
    {
210
        $this->endDate = $endDate;
211
    }
212
213
    /**
214
     * @return string
215
     */
216
    public function getEstimatedEndDate(): string
217
    {
218
        return $this->estimatedEndDate;
219
    }
220
221
    /**
222
     * @param string $estimatedEndDate
223
     */
224
    public function setEstimatedEndDate(string $estimatedEndDate): void
225
    {
226
        $this->estimatedEndDate = $estimatedEndDate;
227
    }
228
229
    /**
230
     * @return Collection
231
     */
232 1
    public function getBookables(): Collection
233
    {
234 1
        return $this->bookables;
235
    }
236
237
    /**
238
     * Add bookable
239
     *
240
     * @param Bookable $bookable
241
     */
242 2
    public function addBookable(Bookable $bookable): void
243
    {
244 2
        if (!$this->bookables->contains($bookable)) {
245 2
            $this->bookables->add($bookable);
246 2
            $bookable->bookingAdded($this);
247
        }
248 2
    }
249
250
    /**
251
     * Remove bookable
252
     *
253
     * @param Bookable $bookable
254
     */
255 1
    public function removeBookable(Bookable $bookable): void
256
    {
257 1
        $this->bookables->removeElement($bookable);
258 1
        $bookable->bookingRemoved($this);
259 1
    }
260
261
    /**
262
     * @API\Field(type="BookingStatus")
263
     *
264
     * @return string
265
     */
266
    public function getStatus(): string
267
    {
268
        return $this->status;
269
    }
270
271
    /**
272
     * @API\Input(type="BookingStatus")
273
     *
274
     * @param string $status
275
     */
276 1
    public function setStatus(string $status): void
277
    {
278 1
        $this->status = $status;
279 1
    }
280
281
    /**
282
     * Mark the booking as terminated with an optional comment,
283
     * but only if not already terminated
284
     *
285
     * @param null|string $comment
286
     */
287
    public function terminate(?string $comment): void
288
    {
289
        // Booking can only be terminated once
290
        if (!$this->getEndDate()) {
291
            $this->setEndDate(Utility::getNow());
292
            if ($comment) {
293
                $this->setEndComment($comment);
294
            }
295
        }
296
    }
297
298
    /**
299
     * @return bool
300
     */
301
    public function getGuest(): bool
302
    {
303
        return $this->guest;
304
    }
305
306
    /**
307
     * @param bool $guest
308
     */
309
    public function setGuest(bool $guest): void
310
    {
311
        $this->guest = $guest;
312
    }
313
}
314