1 | <?php |
||
20 | class Concierge extends Workspace |
||
21 | { |
||
22 | protected $timetable = null; |
||
23 | |||
24 | protected $calendar = null; |
||
25 | |||
26 | protected $booking = null; |
||
27 | |||
28 | protected $vacancies = null; |
||
29 | |||
30 | protected $appointment = null; |
||
31 | |||
32 | 6 | protected function calendar() |
|
33 | { |
||
34 | 6 | if ($this->calendar === null) { |
|
35 | 6 | $this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); |
|
36 | 6 | } |
|
37 | |||
38 | 6 | return $this->calendar; |
|
39 | } |
||
40 | |||
41 | 2 | public function timetable() |
|
49 | |||
50 | 1 | public function vacancies() |
|
58 | |||
59 | 3 | public function booking() |
|
60 | { |
||
61 | 3 | if ($this->booking === null && $this->business !== null) { |
|
62 | 3 | $this->booking = new BookingManager($this->business); |
|
63 | 3 | } |
|
64 | |||
65 | 3 | return $this->booking; |
|
66 | } |
||
67 | |||
68 | 6 | public function takeReservation(array $request) |
|
69 | { |
||
70 | 6 | $issuer = $request['issuer']; |
|
71 | 6 | $service = $request['service']; |
|
72 | 6 | $contact = $request['contact']; |
|
73 | 6 | $comments = $request['comments']; |
|
74 | |||
75 | 6 | $vacancies = $this->calendar() |
|
76 | 6 | ->forService($service->id) |
|
77 | 6 | ->withDuration($service->duration) |
|
78 | 6 | ->forDate($request['date']) |
|
79 | 6 | ->atTime($request['time'], $request['timezone']) |
|
80 | 6 | ->find(); |
|
81 | |||
82 | 6 | if ($vacancies->count() == 0) { |
|
83 | // TODO: Log failure feedback message / raise exception |
||
84 | 2 | return false; |
|
85 | } |
||
86 | |||
87 | 4 | if ($vacancies->count() > 1) { |
|
88 | // Log unexpected behavior message / raise exception |
||
89 | $vacancy = $vacancies->first(); |
||
90 | } |
||
91 | |||
92 | 4 | if ($vacancies->count() == 1) { |
|
93 | 4 | $vacancy = $vacancies->first(); |
|
94 | 4 | } |
|
95 | |||
96 | 4 | $humanresourceId = $vacancy->humanresource ? $vacancy->humanresource->id : null; |
|
|
|||
97 | |||
98 | 4 | $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
|
99 | 4 | $finishAt = $startAt->copy()->addMinutes($service->duration); |
|
100 | |||
101 | 4 | $appointment = $this->generateAppointment( |
|
102 | 4 | $issuer, |
|
103 | 4 | $this->business->id, |
|
104 | 4 | $contact->id, |
|
105 | 4 | $service->id, |
|
106 | 4 | $startAt, |
|
107 | 4 | $finishAt, |
|
108 | 4 | $comments, |
|
109 | $humanresourceId |
||
110 | 4 | ); |
|
111 | |||
112 | /* Should be moved inside generateAppointment() */ |
||
113 | 4 | if ($appointment->duplicates()) { |
|
114 | |||
115 | 2 | $this->appointment = $appointment; |
|
116 | |||
117 | 2 | throw new DuplicatedAppointmentException($appointment->code); |
|
118 | } |
||
119 | |||
120 | 4 | $appointment->vacancy()->associate($vacancy); |
|
121 | 4 | $appointment->save(); |
|
122 | |||
123 | 4 | return $appointment; |
|
124 | } |
||
125 | |||
126 | 4 | protected function generateAppointment( |
|
151 | |||
152 | /** |
||
153 | * Determine if the Business has any published Vacancies available for booking. |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | 2 | public function isBookable($fromDate = 'today', $days = 7) |
|
167 | |||
168 | ////////////////// |
||
169 | // FOR REFACTOR // |
||
170 | ////////////////// |
||
171 | |||
172 | 2 | public function getActiveAppointments() |
|
173 | { |
||
174 | 2 | return $this->business |
|
175 | 2 | ->bookings()->with('contact') |
|
176 | 2 | ->with('business') |
|
177 | 2 | ->with('service') |
|
178 | 2 | ->active() |
|
179 | 2 | ->orderBy('start_at') |
|
180 | 2 | ->get(); |
|
181 | } |
||
182 | |||
183 | public function getUnservedAppointments() |
||
193 | |||
194 | 5 | public function getUnarchivedAppointments() |
|
204 | |||
205 | 4 | protected function makeDateTime($date, $time, $timezone = null) |
|
206 | { |
||
207 | 4 | return Carbon::parse("{$date} {$time} {$timezone}"); |
|
208 | } |
||
209 | |||
210 | 4 | protected function makeDateTimeUTC($date, $time, $timezone = null) |
|
211 | { |
||
212 | 4 | return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); |
|
213 | } |
||
214 | |||
215 | public function appointment() |
||
219 | } |
||
220 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: