Completed
Push — master ( 3cd0a8...94a3ce )
by Ariel
06:26
created

Concierge::generateAppointment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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