Completed
Push — master ( bbe919...15ded1 )
by Ariel
11:12
created

Concierge::takeReservation()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 50
rs 8.6315
cc 5
eloc 29
nc 9
nop 1
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')
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
80
            return false;
81
        }
82
83
        /* Should be moved inside generateAppointment() */
84
        $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...
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