1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Timegridio\Concierge\Calendar\Calendar; |
7
|
|
|
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException; |
8
|
|
|
use Timegridio\Concierge\Models\Appointment; |
9
|
|
|
use Timegridio\Concierge\Models\Business; |
10
|
|
|
use Timegridio\Concierge\Models\Service; |
11
|
|
|
|
12
|
|
|
/******************************************************************************* |
13
|
|
|
* Concierge Service Layer |
14
|
|
|
* High level booking manager |
15
|
|
|
******************************************************************************/ |
16
|
|
|
class Concierge extends Workspace |
17
|
|
|
{ |
18
|
|
|
protected $calendar = null; |
19
|
|
|
|
20
|
|
|
protected $booking = null; |
21
|
|
|
|
22
|
|
|
protected function calendar() |
23
|
|
|
{ |
24
|
|
|
if ($this->calendar === null) { |
25
|
|
|
$this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $this->calendar; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function takeReservation(array $request) |
32
|
|
|
{ |
33
|
|
|
$issuer = $request['issuer']; |
34
|
|
|
$service = $request['service']; |
35
|
|
|
$contact = $request['contact']; |
36
|
|
|
$comments = $request['comments']; |
37
|
|
|
|
38
|
|
|
$vacancies = $this->calendar() |
39
|
|
|
->forService($service->id) |
40
|
|
|
->forDate($request['date']) |
41
|
|
|
->atTime($request['time']) |
42
|
|
|
->find(); |
43
|
|
|
|
44
|
|
|
if ($vacancies->count() == 0) { |
45
|
|
|
// Log failure feedback message |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($vacancies->count() > 1) { |
50
|
|
|
// Log unexpected behavior message |
51
|
|
|
$vacancy = $vacancies->first(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($vacancies->count() == 1) { |
55
|
|
|
$vacancy = $vacancies->first(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
59
|
|
|
$finishAt = $startAt->copy()->addMinutes($service->duration); |
60
|
|
|
|
61
|
|
|
$appointment = $this->generateAppointment( |
62
|
|
|
$issuer, |
63
|
|
|
$this->business->id, |
64
|
|
|
$contact->id, |
65
|
|
|
$service->id, |
66
|
|
|
$startAt, |
67
|
|
|
$finishAt, |
68
|
|
|
$comments |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
/* Should be moved inside generateAppointment() */ |
72
|
|
|
if ($appointment->duplicates()) { |
73
|
|
|
throw new DuplicatedAppointmentException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* Should be moved inside generateAppointment() */ |
77
|
|
|
$appointment->vacancy()->associate($vacancy); |
|
|
|
|
78
|
|
|
$appointment->save(); |
79
|
|
|
|
80
|
|
|
return $appointment; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function generateAppointment( |
84
|
|
|
$issuerId, |
85
|
|
|
$businessId, |
86
|
|
|
$contactId, |
87
|
|
|
$serviceId, |
88
|
|
|
Carbon $startAt, |
89
|
|
|
Carbon $finishAt, |
90
|
|
|
$comments = null) |
91
|
|
|
{ |
92
|
|
|
$appointment = new Appointment(); |
93
|
|
|
|
94
|
|
|
$appointment->doReserve(); |
95
|
|
|
$appointment->setStartAtAttribute($startAt); |
96
|
|
|
$appointment->setFinishAtAttribute($finishAt); |
97
|
|
|
$appointment->business()->associate($businessId); |
98
|
|
|
$appointment->issuer()->associate($issuerId); |
99
|
|
|
$appointment->contact()->associate($contactId); |
100
|
|
|
$appointment->service()->associate($serviceId); |
101
|
|
|
$appointment->comments = $comments; |
|
|
|
|
102
|
|
|
$appointment->doHash(); |
103
|
|
|
|
104
|
|
|
return $appointment; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
protected function makeDateTime($date, $time, $timezone = null) |
108
|
|
|
{ |
109
|
|
|
return Carbon::parse("{$date} {$time} {$timezone}"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function makeDateTimeUTC($date, $time, $timezone = null) |
113
|
|
|
{ |
114
|
|
|
return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
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: