Completed
Push — master ( 1b13d8...567fac )
by Vojta
07:52
created

DatesResolver::getLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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