Completed
Push — master ( bfc36f...e3681f )
by Ariel
13:05
created

Concierge::generateAppointment()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.8571
cc 1
eloc 21
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Timegridio\Concierge;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
7
use Timegridio\Concierge\Booking\BookingManager;
8
use Timegridio\Concierge\Calendar\Calendar;
9
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException;
10
use Timegridio\Concierge\Models\Appointment;
11
use Timegridio\Concierge\Models\Business;
12
use Timegridio\Concierge\Models\Service;
13
use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy;
14
use Timegridio\Concierge\Vacancy\VacancyManager;
15
16
/*******************************************************************************
17
 * Concierge Service Layer
18
 *     High level booking manager
19
 ******************************************************************************/
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()
42
    {
43 2
        if ($this->timetable === null) {
44 2
            $this->timetable = new TimetableStrategy($this->business->strategy);
45 2
        }
46
47 2
        return $this->timetable;
48
    }
49
50 1
    public function vacancies()
51
    {
52 1
        if ($this->vacancies === null && $this->business !== null) {
53 1
            $this->vacancies = new VacancyManager($this->business);
54 1
        }
55
56 1
        return $this->vacancies;
57
    }
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;
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...
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(
127
        $issuerId,
128
        $businessId,
129
        $contactId,
130
        $serviceId,
131
        Carbon $startAt,
132
        Carbon $finishAt,
133
        $comments = null,
134
        $humanresourceId = null)
135
    {
136 4
        $appointment = new Appointment();
137
138 4
        $appointment->doReserve();
139 4
        $appointment->setStartAtAttribute($startAt);
140 4
        $appointment->setFinishAtAttribute($finishAt);
141 4
        $appointment->business()->associate($businessId);
142 4
        $appointment->issuer()->associate($issuerId);
143 4
        $appointment->contact()->associate($contactId);
144 4
        $appointment->service()->associate($serviceId);
145 4
        $appointment->humanresource()->associate($humanresourceId);
146 4
        $appointment->comments = $comments;
147 4
        $appointment->doHash();
148
149 4
        return $appointment;
150
    }
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)
158
    {
159 2
        $timetable = $this->timetable()->buildTimetable($this->business->vacancies, $fromDate, $days);
160
161 2
        $timetable = Arr::flatten($timetable);
162
163 2
        $sum = array_sum($timetable);
164
165 2
        return $sum > 0;
166
    }
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 4
    public function getUnservedAppointments()
184
    {
185
        return $this->business
186
            ->bookings()->with('contact')
187
            ->with('business')
188
            ->with('service')
189
            ->unserved()
190
            ->orderBy('start_at')
191 4
            ->get();
192
    }
193
194 1
    public function getUnarchivedAppointments()
195
    {
196 1
        return $this->business
197 1
            ->bookings()->with('contact')
198 1
            ->with('business')
199 1
            ->with('service')
200 1
            ->unarchived()
201 1
            ->orderBy('start_at')
202 1
            ->get();
203
    }
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()
216
    {
217
        return $this->appointment;
218
    }
219
}
220