Completed
Push — master ( 738917...534a14 )
by Ariel
09:09
created

Concierge   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 13
Bugs 1 Features 3
Metric Value
wmc 12
c 13
b 1
f 3
lcom 1
cbo 7
dl 0
loc 123
ccs 51
cts 51
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A calendar() 0 8 2
A timetable() 0 8 2
B takeReservation() 0 52 4
A generateAppointment() 0 23 1
A isBookable() 0 4 1
A makeDateTime() 0 4 1
A makeDateTimeUTC() 0 4 1
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
use Carbon\Carbon;
6
use Timegridio\Concierge\Calendar\Calendar;
7
use Timegridio\Concierge\Exceptions\DuplicatedAppointmentException;
8
use Timegridio\Concierge\Models\Appointment;
9
use Timegridio\Concierge\Models\Business;
10
use Timegridio\Concierge\Models\Service;
11
use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy;
12
13
/*******************************************************************************
14
 * Concierge Service Layer
15
 *     High level booking manager
16
 ******************************************************************************/
17
class Concierge extends Workspace
18
{
19
    protected $timetable = null;
20
21
    protected $calendar = null;
22 6
23
    protected $booking = null;
24 6
25 6
    protected function calendar()
26 6
    {
27
        if ($this->calendar === null) {
28 6
            $this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone);
29
        }
30
31 6
        return $this->calendar;
32
    }
33 6
34 6
    protected function timetable()
35 6
    {
36 6
        if ($this->timetable === null) {
37
            $this->timetable = new TimetableStrategy($this->business->strategy);
38 6
        }
39 6
40 6
        return $this->timetable;
41 6
    }
42 6
43
    public function takeReservation(array $request)
44 6
    {
45
        $issuer = $request['issuer'];
46 2
        $service = $request['service'];
47
        $contact = $request['contact'];
48
        $comments = $request['comments'];
49
50
        $vacancies = $this->calendar()
51
                          ->forService($service->id)
52
                          ->forDate($request['date'])
53
                          ->atTime($request['time'])
54
                          ->find();
55 4
56 4
        if ($vacancies->count() == 0) {
57 4
            // TODO: Log failure feedback message / raise exception
58
            return false;
59 4
        }
60 4
61
//      DEBUG / INCONSISTENT DB RECORDS CHECK
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% 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...
62 4
//        if ($vacancies->count() > 1) {
63 4
//            // Log unexpected behavior message / raise exception
64 4
//            $vacancy = $vacancies->first();
65 4
//        }
66 4
67 4
        if ($vacancies->count() == 1) {
68 4
            $vacancy = $vacancies->first();
69
        }
70 4
71
        $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']);
72
        $finishAt = $startAt->copy()->addMinutes($service->duration);
73 4
74 2
        $appointment = $this->generateAppointment(
75
            $issuer,
76
            $this->business->id,
77
            $contact->id,
78 4
            $service->id,
79 4
            $startAt,
80
            $finishAt,
81 4
            $comments
82
        );
83
84 4
        /* Should be moved inside generateAppointment() */
85
        if ($appointment->duplicates()) {
86
            throw new DuplicatedAppointmentException();
87
        }
88
89
        /* Should be moved inside generateAppointment() */
90
        $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...
91
        $appointment->save();
92
93 4
        return $appointment;
94
    }
95 4
96 4
    protected function generateAppointment(
97 4
        $issuerId,
98 4
        $businessId,
99 4
        $contactId,
100 4
        $serviceId,
101 4
        Carbon $startAt,
102 4
        Carbon $finishAt,
103 4
        $comments = null)
104
    {
105 4
        $appointment = new Appointment();
106
107
        $appointment->doReserve();
108 4
        $appointment->setStartAtAttribute($startAt);
109
        $appointment->setFinishAtAttribute($finishAt);
110 4
        $appointment->business()->associate($businessId);
111
        $appointment->issuer()->associate($issuerId);
112
        $appointment->contact()->associate($contactId);
113 4
        $appointment->service()->associate($serviceId);
114
        $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...
115 4
        $appointment->doHash();
116
117
        return $appointment;
118
    }
119
120
    /**
121
     * Determine if the Business has any published Vacancies available for booking.
122
     * 
123
     * @return boolean
124
     */
125
    public function isBookable()
126
    {
127
        return count($this->timetable()->buildTimetable($this->business->vacancies)) > 0;
128
    }
129
130
    protected function makeDateTime($date, $time, $timezone = null)
131
    {
132
        return Carbon::parse("{$date} {$time} {$timezone}");
133
    }
134
135
    protected function makeDateTimeUTC($date, $time, $timezone = null)
136
    {
137
        return $this->makeDateTime($date, $time, $timezone)->timezone('UTC');
138
    }
139
}
140