Completed
Push — master ( 076ecd...1e6ec2 )
by Ariel
13:10
created

Concierge::booking()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
use Carbon\Carbon;
6
use Timegridio\Concierge\Booking\BookingManager;
7
use Timegridio\Concierge\Calendar\Calendar;
8
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException;
9
use Timegridio\Concierge\Models\Appointment;
10
use Timegridio\Concierge\Models\Business;
11
use Timegridio\Concierge\Models\Service;
12
use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy;
13
use Timegridio\Concierge\Vacancy\VacancyManager;
14
15
/*******************************************************************************
16
 * Concierge Service Layer
17
 *     High level booking manager
18
 ******************************************************************************/
19
class Concierge extends Workspace
20
{
21
    protected $timetable = null;
22
23
    protected $calendar = null;
24
25
    protected $booking = null;
26
27
    protected $vacancies = null;
28
29
    protected $appointment = null;
30
31 6
    protected function calendar()
32
    {
33 6
        if ($this->calendar === null) {
34 6
            $this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone);
35 6
        }
36
37 6
        return $this->calendar;
38
    }
39
40
    public function timetable()
41
    {
42
        if ($this->timetable === null) {
43
            $this->timetable = new TimetableStrategy($this->business->strategy);
44
        }
45
46
        return $this->timetable;
47
    }
48
49 1
    public function vacancies()
50
    {
51 1
        if ($this->vacancies === null && $this->business !== null) {
52 1
            $this->vacancies = new VacancyManager($this->business);
53 1
        }
54
55 1
        return $this->vacancies;
56
    }
57
58 3
    public function booking()
59
    {
60 3
        if ($this->booking === null && $this->business !== null) {
61 3
            $this->booking = new BookingManager($this->business);
62 3
        }
63
64 3
        return $this->booking;
65
    }
66
67 6
    public function takeReservation(array $request)
68
    {
69 6
        $issuer = $request['issuer'];
70 6
        $service = $request['service'];
71 6
        $contact = $request['contact'];
72 6
        $comments = $request['comments'];
73
74 6
        $vacancies = $this->calendar()
75 6
                          ->forService($service->id)
76 6
                          ->withDuration($service->duration)
77 6
                          ->forDate($request['date'])
78 6
                          ->atTime($request['time'], $request['timezone'])
79 6
                          ->find();
80
81 6
        if ($vacancies->count() == 0) {
82
            // TODO: Log failure feedback message / raise exception
83 2
            return false;
84
        }
85
86 4
        if ($vacancies->count() > 1) {
87
            // Log unexpected behavior message / raise exception
88
            $vacancy = $vacancies->first();
89
        }
90
91 4
        if ($vacancies->count() == 1) {
92 4
            $vacancy = $vacancies->first();
93 4
        }
94
95 4
        $humanresourceId = $vacancy->humanresource ? $vacancy->humanresource->id : null;
0 ignored issues
show
Bug introduced by
The variable $vacancy does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
96
97 4
        $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']);
98 4
        $finishAt = $startAt->copy()->addMinutes($service->duration);
99
100 4
        $appointment = $this->generateAppointment(
101 4
            $issuer,
102 4
            $this->business->id,
103 4
            $contact->id,
104 4
            $service->id,
105 4
            $startAt,
106 4
            $finishAt,
107 4
            $comments,
108
            $humanresourceId
109 4
        );
110
111
        /* Should be moved inside generateAppointment() */
112 4
        if ($appointment->duplicates()) {
113 2
            $this->appointment = $appointment;
114
115 2
            throw new DuplicatedAppointmentException($appointment->code);
116
        }
117
118 4
        $appointment->vacancy()->associate($vacancy);
119 4
        $appointment->save();
120
121 4
        return $appointment;
122
    }
123
124 4
    protected function generateAppointment(
125
        $issuerId,
126
        $businessId,
127
        $contactId,
128
        $serviceId,
129
        Carbon $startAt,
130
        Carbon $finishAt,
131
        $comments = null,
132
        $humanresourceId = null)
133
    {
134 4
        $appointment = new Appointment();
135
136 4
        $appointment->doReserve();
137 4
        $appointment->setStartAtAttribute($startAt);
138 4
        $appointment->setFinishAtAttribute($finishAt);
139 4
        $appointment->business()->associate($businessId);
140 4
        $appointment->issuer()->associate($issuerId);
141 4
        $appointment->contact()->associate($contactId);
142 4
        $appointment->service()->associate($serviceId);
143 4
        $appointment->humanresource()->associate($humanresourceId);
144 4
        $appointment->comments = $comments;
145 4
        $appointment->doHash();
146
147 4
        return $appointment;
148
    }
149
150
    /**
151
     * Determine if the Business has any published Vacancies available for booking.
152
     *
153
     * @param string $fromDate
154
     * @param int $days
155
     *
156
     * @return bool
157
     */
158 2
    public function isBookable($fromDate = 'today', $days = 7)
159
    {
160 2
        $fromDate = Carbon::parse($fromDate)->timezone($this->business->timezone);
161
162 2
        $count = $this->business
163 2
                      ->vacancies()
164 2
                      ->future($fromDate)
165 2
                      ->until($fromDate->addDays($days))
166 2
                      ->count();
167
168 2
        return $count > 0;
169
    }
170
171
    //////////////////
172
    // FOR REFACTOR //
173
    //////////////////
174
175 2
    public function getActiveAppointments()
176
    {
177 2
        return $this->business
178 2
            ->bookings()->with('contact')
179 2
            ->with('business')
180 2
            ->with('service')
181 2
            ->active()
182 2
            ->orderBy('start_at')
183 2
            ->get();
184
    }
185
186
    public function getUnservedAppointments()
187
    {
188
        return $this->business
189
            ->bookings()->with('contact')
190
            ->with('business')
191
            ->with('service')
192
            ->unserved()
193
            ->orderBy('start_at')
194
            ->get();
195
    }
196
197 1
    public function getUnarchivedAppointments()
198
    {
199 1
        return $this->business
200 1
            ->bookings()->with('contact')
201 1
            ->with('business')
202 1
            ->with('service')
203 1
            ->unarchived()
204 1
            ->orderBy('start_at')
205 1
            ->get();
206
    }
207
208 4
    protected function makeDateTime($date, $time, $timezone = null)
209
    {
210 4
        return Carbon::parse("{$date} {$time} {$timezone}");
211
    }
212
213 4
    protected function makeDateTimeUTC($date, $time, $timezone = null)
214
    {
215 4
        return $this->makeDateTime($date, $time, $timezone)->timezone('UTC');
216
    }
217
218
    public function appointment()
219
    {
220
        return $this->appointment;
221
    }
222
}
223