BookingConfirmation::getDepartureDate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Gueststream\Reservations;
4
5
use DateTime;
6
use DateTimeZone;
7
use Exception;
8
use JsonSerializable;
9
10
class BookingConfirmation implements JsonSerializable
11
{
12
13
    /**
14
     * @var string
15
     */
16
    protected $bookingNumber;
17
18
    /**
19
     * @var string
20
     */
21
    protected $altBookingNumber;
22
23
    /**
24
     * @var DateTime
25
     */
26
    protected $arrivalDate;
27
28
    /**
29
     * @var DateTime
30
     */
31
    protected $departureDate;
32
33
    /**
34
     * @var int
35
     */
36
    protected $nights;
37
38
    /**
39
     * @var float
40
     */
41
    protected $totalCost;
42
43
    /**
44
     * The Total amount charged to the guest's credit card.
45
     *
46
     * @var float
47
     */
48
    protected $totalCharged;
49
50
    /**
51
     * @var boolean
52
     */
53
    protected $hasTravelInsurance = false;
54
55
    /**
56 33
     * @var boolean
57
     */
58 33
    protected $hasDamageInsurance = false;
59 3
60 3
    /**
61 33
     * @param array|object|null $bookingConfirmationData
62
     */
63 3
    public function __construct($bookingConfirmationData = null)
64
    {
65 3
        if (!is_null($bookingConfirmationData)) {
66 3
            $this->hydrateBookingConfirmation($bookingConfirmationData);
67
        }
68 3
    }
69 3
70 3
    public function hydrateBookingConfirmation($bookingConfirmationData)
71 3
    {
72 3
        foreach ($bookingConfirmationData as $key => $value) {
73
            $setterMethodName = "set" . ucfirst($key);
74
75
            if (method_exists($this, $setterMethodName)) {
76
                $this->$setterMethodName($value);
77 9
            }
78
        }
79 9
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getBookingNumber()
85 9
    {
86
        return $this->bookingNumber;
87 9
    }
88 9
89
    /**
90
     * @param string $bookingNumber
91
     */
92
    public function setBookingNumber($bookingNumber)
93 9
    {
94
        $this->bookingNumber = $bookingNumber;
95 9
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getAltBookingNumber()
101 9
    {
102
        return $this->altBookingNumber;
103 9
    }
104 9
105
    /**
106
     * @param string $altBookingNumber
107
     */
108
    public function setAltBookingNumber($altBookingNumber)
109
    {
110 12
        $this->altBookingNumber = $altBookingNumber;
111
    }
112 12
113 3
    /**
114
     * @return DateTime
115
     * @throws Exception
116 9
     */
117
    public function getArrivalDate()
118
    {
119
        if (empty($this->arrivalDate)) {
120
            throw new Exception('Arrival date must first be set.');
121
        }
122 12
123
        return $this->arrivalDate;
124 12
    }
125 3
126
    /**
127 3
     * @param DateTime $arrivalDate
128
     */
129
    public function setArrivalDate($arrivalDate)
130 9
    {
131 9
        if ($arrivalDate instanceof DateTime) {
132 9
            $this->arrivalDate = $arrivalDate;
133
134
            return;
135
        }
136
137
        $this->arrivalDate = new DateTime($arrivalDate, new DateTimeZone('UTC'));
138 12
        $this->updateNights();
139
    }
140 12
141 3
    /**
142
     * @return DateTime
143
     * @throws Exception
144 9
     */
145
    public function getDepartureDate()
146
    {
147
        if (empty($this->departureDate)) {
148
            throw new Exception('Departure date must first be set.');
149
        }
150 12
151
        return $this->departureDate;
152 12
    }
153 3
154
    /**
155 3
     * @param DateTime $departureDate
156
     */
157
    public function setDepartureDate($departureDate)
158 9
    {
159 9
        if ($departureDate instanceof DateTime) {
160 9
            $this->departureDate = $departureDate;
161
162 9
            return;
163
        }
164 9
165 9
        $this->departureDate = new DateTime($departureDate, new DateTimeZone('UTC'));
166 9
        $this->updateNights();
167
    }
168 9
169
    protected function updateNights()
170
    {
171 9
        if (!empty($this->arrivalDate) && !empty($this->departureDate)) {
172 9
            $nightsDiff   = $this->arrivalDate->diff($this->departureDate);
173
            $this->nights = (int) $nightsDiff->format('%a');
174
175
            return;
176
        }
177 3
178
        $this->nights = null;
179 3
    }
180
181
    /**
182
     * @return int
183
     */
184
    public function getNights()
185 9
    {
186
        return $this->nights;
187 9
    }
188
189
    /**
190
     * @return float
191
     */
192
    public function getTotalCost()
193 9
    {
194
        return $this->totalCost;
195 9
    }
196 9
197
    /**
198
     * @param float $totalCost
199
     */
200
    public function setTotalCost($totalCost)
201 9
    {
202
        $this->totalCost = $totalCost;
203 9
    }
204
205
    /**
206
     * @return float
207
     */
208
    public function getTotalCharged()
209 9
    {
210
        return $this->totalCharged;
211 9
    }
212 9
213
    /**
214
     * @param float $totalCharged
215
     */
216
    public function setTotalCharged($totalCharged)
217 9
    {
218
        $this->totalCharged = $totalCharged;
219 9
    }
220
221
    /**
222
     * @return boolean
223
     */
224
    public function hasTravelInsurance()
225 9
    {
226
        return $this->hasTravelInsurance;
227 9
    }
228 9
229
    /**
230 6
     * @param boolean $hasTravelInsurance
231
     */
232
    public function setHasTravelInsurance($hasTravelInsurance = true)
233 6
    {
234 6
        $this->hasTravelInsurance = (boolean) $hasTravelInsurance;
235 6
    }
236 6
237 6
    /**
238 6
     * @return boolean
239 6
     */
240 6
    public function hasDamageInsurance()
241
    {
242 6
        return $this->hasDamageInsurance;
243
    }
244
245
    /**
246
     * @param boolean $hasDamageInsurance
247
     */
248
    public function setHasDamageInsurance($hasDamageInsurance = true)
249
    {
250
        $this->hasDamageInsurance = (boolean) $hasDamageInsurance;
251
    }
252
253
    public function jsonSerialize()
254
    {
255
        $bookingConfirmationData = [
256
            'bookingNumber'      => $this->getBookingNumber(),
257
            'altBookingNumber'   => $this->getAltBookingNumber(),
258
            'totalCost'          => $this->getTotalCost(),
259
            'hasDamageInsurance' => $this->hasDamageInsurance(),
260
            'hasTravelInsurance' => $this->hasTravelInsurance(),
261
            'arrivalDate'        => $this->getArrivalDate()->format('c'),
262
            'departureDate'      => $this->getDepartureDate()->format('c'),
263
        ];
264
265
        return $bookingConfirmationData;
266
    }
267
}
268