Passed
Pull Request — master (#20)
by
unknown
08:32
created

DatesResolver::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 2
1
<?php namespace VojtaSvoboda\Reservations\Classes;
2
3
use Carbon\Carbon;
4
use Config;
5
use Illuminate\Database\Eloquent\Collection;
6
use VojtaSvoboda\Reservations\Models\Settings;
7
8
/**
9
 * Class DatesResolver transform reservations to booked time slots, grouped by date.
10
 *
11
 * @package VojtaSvoboda\Reservations\Classes
12
 */
13
class DatesResolver
14
{
15
    /**
16
     * Returns reserved time slots from non cancelled reservations.
17
     *
18
     * If you have reservations at 13.10.2016 at 11:00 and also at 13:00, both with 2 hours lenght, this method return
19
     * all booked slots - from 09:15 to 14:45 (when you have 15 minutes slot lenght).
20
     *
21
     * ------------ 11:00 ------------- 13:00 --------------
22
     *
23
     * Because nearest reservation can be done at 09:00 with 2 hours lenghts and next reservation at 15:00.
24
     *
25
     * @param Collection $reservations
26
     *
27
     * @return array
28
     */
29
    public function getDatesFromReservations(Collection $reservations)
30
    {
31
        // init
32
        $interval = $this->getInterval();
33
        $length = $this->getLength();
34
        $dateFormat = 'd/m/Y';
35
        $timeFormat = 'H:i';
36
37
        // sort reservations by date and count time slots before reservation and during the reservation
38
        $dates = [];
39
        foreach ($reservations as $reservation) {
40
            // init dates
41
            $startDate = $this->getStartDate($reservation, $length, $interval);
42
            $endDate = $this->getEndDate($reservation, $length);
43
44
            // save each booked interval
45
            while ($startDate < $endDate) {
46
                $time = $startDate->format($timeFormat);
47
                $date = $startDate->format($dateFormat);
48
                $dates[$date][$time] = $time;
49
                $startDate->modify('+' . $interval . ' minutes');
50
            }
51
        }
52
53
        return $dates;
54
    }
55
56
    /**
57
     * Get booked interval around the given date.
58
     *
59
     * @param Carbon $date
60
     *
61
     * @return array
62 11
     */
63
    public function getBoundaryDates(Carbon $date)
64
    {
65 11
        // reservation length
66
        $length = $this->getLength();
67
68 11
        // boundary dates before and after
69 11
        $startDatetime = $this->getBoundaryBefore($date, $length);
70
        $endDatetime = $this->getBoundaryAfter($date, $length);
71 11
72
        return [$startDatetime, $endDatetime];
73
    }
74
75
    /**
76
     * Get boundary date before reservation date.
77
     *
78
     * @param Carbon $date
79
     * @param string $length
80
     *
81
     * @return mixed
82 11
     */
83
    private function getBoundaryBefore(Carbon $date, $length)
84 11
    {
85 11
        $startDatetime = clone $date;
86 11
        $startDatetime->modify('-' . $length);
87
        $startDatetime->modify('+1 second');
88 11
89
        return $startDatetime;
90
    }
91
92
    /**
93
     * Get boundary date after reservation date.
94
     *
95
     * @param Carbon $date
96
     * @param string $length
97
     *
98
     * @return mixed
99 11
     */
100
    private function getBoundaryAfter(Carbon $date, $length)
101 11
    {
102 11
        $endDatetime = clone $date;
103 11
        $endDatetime->modify('+' . $length);
104
        $endDatetime->modify('-1 second');
105 11
106
        return $endDatetime;
107
    }
108
109
    /**
110
     * Get reservation imaginary start date.
111
     *
112
     * @param $reservation
113
     * @param $length
114
     * @param $interval
115
     *
116
     * @return mixed
117
     */
118
    protected function getStartDate($reservation, $length, $interval)
119
    {
120
        $startDate = $reservation->date;
121
        $startDate->modify('-' . $length);
122
        $startDate->modify('+' . $interval . ' minutes');
123
124
        return $startDate;
125
    }
126
127
    /**
128
     * Get reservation imaginary end date.
129
     *
130
     * @param $reservation
131
     * @param $length
132
     *
133
     * @return mixed
134
     */
135
    protected function getEndDate($reservation, $length)
136
    {
137
        $endDate = clone $reservation->date;
138
        $endDate->modify('+' . $length);
139
140
        return $endDate;
141
    }
142
143
    protected function getDateFormat()
144
    {
145
        return Settings::get(
146
            'formats_date',
147
            Config::get('vojtasvoboda.reservations::config.formats.date', 'd/m/Y')
148
        );
149
    }
150
151
    protected function getTimeFormat()
152
    {
153
        return Settings::get(
154
            'formats_time',
155
            Config::get('vojtasvoboda.reservations::config.formats.time', 'H:i')
156
        );
157 11
    }
158
159 11
    protected function getInterval()
160
    {
161
        return Settings::get(
162
            'reservation_interval',
163
            Config::get('vojtasvoboda.reservations::config.reservation.interval', 15)
164
        );
165
    }
166
167
    protected function getLength()
168
    {
169
        return Settings::get(
170
            'reservation_length',
171
            (int)Config::get('vojtasvoboda.reservations::config.reservation.length', 2)
172
        ).' hours';
173
    }
174
}
175