Completed
Push — master ( 6cb927...1b49b3 )
by Ariel
06:13
created

Concierge::takeReservation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 49
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 49
rs 9.2258
cc 3
eloc 24
nc 3
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
        $datetime = $this->makeDateTimeUTC($request['date'], $request['time'], $request['timezone']);
58
59
        $appointment = $this->booking()->generateAppointment(
60
            $issuer,
61
            $this->business,
62
            $contact,
63
            $service,
64
            $datetime,
65
            $comments
66
        );
67
68
        if ($appointment->duplicates()) {
69
            // 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...
70
            return false;
71
        }
72
73
        $appointment->save();
74
75
        return $appointment;
76
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% 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...
77
//        $vacancy = $this->calendar()->getSlotFor($appointment->start_at, $appointment->finish_at, $appointment->service->id);
78
//
79
//        if ($vacancy != null) {
80
//            $appointment->vacancy()->associate($vacancy);
81
//            $appointment->save();
82
//
83
//            return $appointment;
84
//        }
85
//
86
//        return false;
87
    }
88
89
    protected function makeDateTime($date, $time, $timezone = null)
90
    {
91
        return Carbon::parse("{$date} {$time} {$timezone}");
92
    }
93
94
    protected function makeDateTimeUTC($date, $time, $timezone = null)
95
    {
96
        return $this->makeDateTime($date, $time, $timezone)->timezone('UTC');
97
    }
98
}
99