Completed
Push — master ( 877ae8...0439d3 )
by Ariel
09:56
created

Concierge::vacancies()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
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
use Timegridio\Concierge\Timetable\Strategies\TimetableStrategy;
13
use Timegridio\Concierge\Vacancy\VacancyManager;
14
15
/*******************************************************************************
16
 * Concierge Service Layer
17
 *     High level booking manager
18
 ******************************************************************************/
19
class Concierge extends Workspace
20
{
21
    protected $timetable = null;
22
23
    protected $calendar = null;
24
25
    protected $booking = null;
26 7
27
    protected $vacancies = null;
28 7
29 6
    protected function calendar()
30 6
    {
31
        if ($this->calendar === null) {
32 6
            $this->calendar = new Calendar($this->business->strategy, $this->business->vacancies(), $this->business->timezone);
33
        }
34
35 2
        return $this->calendar;
36
    }
37 2
38 2
    public function timetable()
39 2
    {
40
        if ($this->timetable === null) {
41 2
            $this->timetable = new TimetableStrategy($this->business->strategy);
42
        }
43
44 6
        return $this->timetable;
45
    }
46 6
47 6
    public function vacancies()
48 6
    {
49 6
        if ($this->vacancies === null && $this->business !== null) {
50
            $this->vacancies = new VacancyManager($this->business);
51 6
        }
52 6
53 6
        return $this->vacancies;
54 6
    }
55 6
56
    public function takeReservation(array $request)
57 6
    {
58
        $issuer = $request['issuer'];
59 2
        $service = $request['service'];
60
        $contact = $request['contact'];
61
        $comments = $request['comments'];
62
63
        $vacancies = $this->calendar()
64
                          ->forService($service->id)
65
                          ->withDuration($service->duration)
66
                          ->forDate($request['date'])
67
                          ->atTime($request['time'])
68 4
                          ->find();
69 4
70 4
        if ($vacancies->count() == 0) {
71
            // TODO: Log failure feedback message / raise exception
72 4
            return false;
73 4
        }
74
75 4
//      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...
76 4
//        if ($vacancies->count() > 1) {
77 4
//            // Log unexpected behavior message / raise exception
78 4
//            $vacancy = $vacancies->first();
79 4
//        }
80 4
81 4
        if ($vacancies->count() == 1) {
82
            $vacancy = $vacancies->first();
83 4
        }
84
85
        $startAt = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']);
86 4
        $finishAt = $startAt->copy()->addMinutes($service->duration);
87 2
88
        $appointment = $this->generateAppointment(
89
            $issuer,
90
            $this->business->id,
91 4
            $contact->id,
92 4
            $service->id,
93
            $startAt,
94 4
            $finishAt,
95
            $comments
96
        );
97 4
98
        /* Should be moved inside generateAppointment() */
99
        if ($appointment->duplicates()) {
100
            throw new DuplicatedAppointmentException();
101
        }
102
103
        /* Should be moved inside generateAppointment() */
104
        $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...
105
        $appointment->save();
106 4
107
        return $appointment;
108 4
    }
109 4
110 4
    protected function generateAppointment(
111 4
        $issuerId,
112 4
        $businessId,
113 4
        $contactId,
114 4
        $serviceId,
115 4
        Carbon $startAt,
116 4
        Carbon $finishAt,
117
        $comments = null)
118 4
    {
119
        $appointment = new Appointment();
120
121
        $appointment->doReserve();
122
        $appointment->setStartAtAttribute($startAt);
123
        $appointment->setFinishAtAttribute($finishAt);
124
        $appointment->business()->associate($businessId);
125
        $appointment->issuer()->associate($issuerId);
126 2
        $appointment->contact()->associate($contactId);
127
        $appointment->service()->associate($serviceId);
128 2
        $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...
129
        $appointment->doHash();
130 2
131
        return $appointment;
132 2
    }
133
134 2
    /**
135
     * Determine if the Business has any published Vacancies available for booking.
136
     *
137 4
     * @return bool
138
     */
139 4
    public function isBookable($fromDate = 'today', $days = 7)
140
    {
141
        $timetable = $this->timetable()->buildTimetable($this->business->vacancies, $fromDate, $days);
142 4
143
        $timetable = Arr::flatten($timetable);
144 4
145
        $sum = array_sum($timetable);
146
147
        return $sum > 0;
148
    }
149
150
    protected function makeDateTime($date, $time, $timezone = null)
151
    {
152
        return Carbon::parse("{$date} {$time} {$timezone}");
153
    }
154
155
    protected function makeDateTimeUTC($date, $time, $timezone = null)
156
    {
157
        return $this->makeDateTime($date, $time, $timezone)->timezone('UTC');
158
    }
159
}
160