Completed
Push — master ( cb5fa1...8201f5 )
by Vojta
06:45
created

DatesResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 69
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getDatesFromReservations() 0 32 3
A getDateFormat() 0 4 1
A getTimeFormat() 0 4 1
A getInterval() 0 4 1
A getLength() 0 4 1
1
<?php namespace VojtaSvoboda\Reservations\Classes;
2
3
use Carbon\Carbon;
4
use Config;
5
use Illuminate\Database\Eloquent\Collection;
6
7
/**
8
 * Class DatesResolver transform reservations to booked time slots, grouped by date.
9
 *
10
 * @package VojtaSvoboda\Reservations\Classes
11
 */
12
class DatesResolver
13
{
14
    /**
15
     * Returns reserved time slots from non cancelled reservations.
16
     *
17
     * If you have reservations at 13.10.2016 at 11:00 and also at 13:00, both with 2 hours lenght, this method return
18
     * all booked slots - from 09:15 to 14:45 (when you have 15 minutes slot lenght).
19
     *
20
     * ------------ 11:00 ------------- 13:00 --------------
21
     *
22
     * Because nearest reservation can be done at 09:00 with 2 hours lenghts and next reservation at 15:00.
23
     *
24
     * @param Collection $reservations
25
     *
26
     * @return array
27
     */
28
    public function getDatesFromReservations(Collection $reservations)
29
    {
30
        // init
31
        $interval = $this->getInterval();
32
        $length = $this->getLength();
33
        $dateFormat = $this->getDateFormat();
34
        $timeFormat = $this->getTimeFormat();
35
36
        // sort reservations by date and count time slots before reservation and during the reservation
37
        $dates = [];
38
        foreach ($reservations as $reservation)
39
        {
40
            /** @var Carbon $date */
41
            $date = Carbon::parse($reservation->date);
42
            $reservationDay = $date->format($dateFormat);
43
44
            // reservation end date
45
            $endTime = clone $date;
46
            $endTime->modify('+' . $length);
47
48
            // each reservation takes some time
49
            $date->modify('-' . $length);
50
            $date->modify('+' . $interval . ' minutes');
51
            while ($date < $endTime) {
52
                $time = $date->format($timeFormat);
53
                $dates[$reservationDay][$time] = $time;
54
                $date->modify('+' . $interval . ' minutes');
55
            }
56
        }
57
58
        return $dates;
59
    }
60
61
    private function getDateFormat()
62
    {
63
        return Config::get('vojtasvoboda.reservations::config.formats.date', 'd/m/Y');
64
    }
65
66
    private function getTimeFormat()
67
    {
68
        return Config::get('vojtasvoboda.reservations::config.formats.time', 'H:i');
69
    }
70
71
    private function getInterval()
72
    {
73
        return Config::get('vojtasvoboda.reservations::config.reservation.interval', 15);
74
    }
75
76
    private function getLength()
77
    {
78
        return Config::get('vojtasvoboda.reservations::config.reservation.length', '2 hours');
79
    }
80
}
81