Completed
Push — master ( 6ea7ff...8c9744 )
by Ariel
11:07
created

Concierge   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 86.73%

Importance

Changes 23
Bugs 2 Features 7
Metric Value
wmc 23
c 23
b 2
f 7
lcom 1
cbo 10
dl 0
loc 191
ccs 85
cts 98
cp 0.8673
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A calendar() 0 8 2
A timetable() 0 8 2
A vacancies() 0 8 3
A booking() 0 8 3
B takeReservation() 0 59 6
B generateAppointment() 0 25 1
A isBookable() 0 10 1
A getActiveAppointments() 0 10 1
A getUnservedAppointments() 0 10 1
A makeDateTime() 0 4 1
A makeDateTimeUTC() 0 4 1
A appointment() 0 4 1
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'])
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
            dd('TEST');
90
            $vacancy = $vacancies->first();
91
        }
92
93 4
        if ($vacancies->count() == 1) {
94 4
            $vacancy = $vacancies->first();
95 4
        }
96
97 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...
98
99 4
        $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']);
100 4
        $finishAt = $startAt->copy()->addMinutes($service->duration);
101
102 4
        $appointment = $this->generateAppointment(
103 4
            $issuer,
104 4
            $this->business->id,
105 4
            $contact->id,
106 4
            $service->id,
107 4
            $startAt,
108 4
            $finishAt,
109 4
            $comments,
110
            $humanresourceId
111 4
        );
112
113
        /* Should be moved inside generateAppointment() */
114 4
        if ($appointment->duplicates()) {
115
            
116 2
            $this->appointment = $appointment;
117
118 2
            throw new DuplicatedAppointmentException($appointment->code);
0 ignored issues
show
Documentation introduced by
The property code does not exist on object<Timegridio\Concierge\Models\Appointment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
119
        }
120
121
        /* Should be moved inside generateAppointment() */
122 4
        $appointment->vacancy()->associate($vacancy);
123 4
        $appointment->save();
124
125 4
        return $appointment;
126
    }
127
128 4
    protected function generateAppointment(
129
        $issuerId,
130
        $businessId,
131
        $contactId,
132
        $serviceId,
133
        Carbon $startAt,
134
        Carbon $finishAt,
135
        $comments = null,
136
        $humanresourceId = null)
137
    {
138 4
        $appointment = new Appointment();
139
140 4
        $appointment->doReserve();
141 4
        $appointment->setStartAtAttribute($startAt);
142 4
        $appointment->setFinishAtAttribute($finishAt);
143 4
        $appointment->business()->associate($businessId);
144 4
        $appointment->issuer()->associate($issuerId);
145 4
        $appointment->contact()->associate($contactId);
146 4
        $appointment->service()->associate($serviceId);
147 4
        $appointment->humanresource()->associate($humanresourceId);
148 4
        $appointment->comments = $comments;
0 ignored issues
show
Documentation introduced by
The property comments does not exist on object<Timegridio\Concierge\Models\Appointment>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149 4
        $appointment->doHash();
150
151 4
        return $appointment;
152
    }
153
154
    /**
155
     * Determine if the Business has any published Vacancies available for booking.
156
     *
157
     * @return bool
158
     */
159 2
    public function isBookable($fromDate = 'today', $days = 7)
160
    {
161 2
        $timetable = $this->timetable()->buildTimetable($this->business->vacancies, $fromDate, $days);
162
163 2
        $timetable = Arr::flatten($timetable);
164
165 2
        $sum = array_sum($timetable);
166
167 2
        return $sum > 0;
168
    }
169
170
    //////////////////
171
    // FOR REFACTOR //
172
    //////////////////
173
174 2
    public function getActiveAppointments()
175
    {
176 2
        return $this->business
177 2
            ->bookings()->with('contact')
178 2
            ->with('business')
179 2
            ->with('service')
180 2
            ->active()
181 2
            ->orderBy('start_at')
182 2
            ->get();
183
    }
184
185
    public function getUnservedAppointments()
186
    {
187
        return $this->business
188
            ->bookings()->with('contact')
189
            ->with('business')
190
            ->with('service')
191
            ->unserved()
192
            ->orderBy('start_at')
193
            ->get();
194
    }
195
196 4
    protected function makeDateTime($date, $time, $timezone = null)
197
    {
198 4
        return Carbon::parse("{$date} {$time} {$timezone}");
199
    }
200
201 4
    protected function makeDateTimeUTC($date, $time, $timezone = null)
202
    {
203 4
        return $this->makeDateTime($date, $time, $timezone)->timezone('UTC');
204
    }
205
206
    public function appointment()
207
    {
208
        return $this->appointment;
209
    }
210
}
211