1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Timegridio\Concierge\Booking\Strategies\BookingStrategy; |
7
|
|
|
use Timegridio\Concierge\Calendar\Calendar; |
8
|
|
|
use Timegridio\Concierge\Models\Business; |
9
|
|
|
use Timegridio\Concierge\Models\Service; |
10
|
|
|
|
11
|
|
|
/******************************************************************************* |
12
|
|
|
* Concierge Service Layer |
13
|
|
|
* High level booking manager |
14
|
|
|
******************************************************************************/ |
15
|
|
|
class Concierge extends Workspace |
16
|
|
|
{ |
17
|
|
|
protected $calendar = null; |
18
|
|
|
|
19
|
|
|
protected $booking = null; |
20
|
|
|
|
21
|
|
|
protected function calendar() |
22
|
|
|
{ |
23
|
|
|
if ($this->calendar === null) { |
24
|
|
|
$this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->calendar; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function booking() |
31
|
|
|
{ |
32
|
|
|
if ($this->booking === null) { |
33
|
|
|
$this->booking = new BookingStrategy($this->business->strategy); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->booking; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function takeReservation($request) |
40
|
|
|
{ |
41
|
|
|
$issuer = $request['issuer']; |
42
|
|
|
$service = $request['service']; |
43
|
|
|
$contact = $request['contact']; |
44
|
|
|
$comments = $request['comments']; |
45
|
|
|
|
46
|
|
|
$vacancies = $this->calendar() |
47
|
|
|
->forService($service->id) |
48
|
|
|
->forDate($request['date']) |
49
|
|
|
->atTime($request['time']) |
50
|
|
|
->find(); |
51
|
|
|
|
52
|
|
|
if ($vacancies->count() == 0) { |
53
|
|
|
// Log failure feedback message |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($vacancies->count() > 1) { |
58
|
|
|
// Log unexpected behavior message |
59
|
|
|
$vacancy = $vacancies->first(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if ($vacancies->count() == 1) { |
63
|
|
|
$vacancy = $vacancies->first(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$datetime = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']); |
67
|
|
|
|
68
|
|
|
$appointment = $this->booking()->generateAppointment( |
69
|
|
|
$issuer, |
70
|
|
|
$this->business, |
71
|
|
|
$contact, |
72
|
|
|
$service, |
73
|
|
|
$datetime, |
74
|
|
|
$comments |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
/* Should be moved inside generateAppointment() */ |
78
|
|
|
if ($appointment->duplicates()) { |
79
|
|
|
// Throw Exception('Duplicated Appointment Attempted') |
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/* Should be moved inside generateAppointment() */ |
84
|
|
|
$appointment->vacancy()->associate($vacancy); |
|
|
|
|
85
|
|
|
$appointment->save(); |
86
|
|
|
|
87
|
|
|
return $appointment; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function makeDateTime($date, $time, $timezone = null) |
91
|
|
|
{ |
92
|
|
|
return Carbon::parse("{$date} {$time} {$timezone}"); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function makeDateTimeUTC($date, $time, $timezone = null) |
96
|
|
|
{ |
97
|
|
|
return $this->makeDateTime($date, $time, $timezone)->timezone('UTC'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.