Completed
Push — develop ( f7640a...3c81d1 )
by Abdelrahman
01:12
created

IsBookingUser::bookingsCancelledAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Bookings\Traits;
6
7
use Carbon\Carbon;
8
use Rinvex\Bookings\Models\Booking;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
12
trait IsBookingUser
13
{
14
    /**
15
     * The user may have many bookings.
16
     *
17
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
18
     */
19
    public function bookings(): HasMany
20
    {
21
        return $this->hasMany(config('rinvex.bookings.models.booking'), 'user_id', 'id');
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
    }
23
24
    /**
25
     * Get past bookings.
26
     *
27
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
28
     */
29
    public function pastBookings(): HasMany
30
    {
31
        return $this->bookings()
32
                    ->whereNull('cancelled_at')
33
                    ->whereNotNull('ends_at')
34
                    ->where('ends_at', '<', Carbon::now());
35
    }
36
37
    /**
38
     * Get future bookings.
39
     *
40
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
41
     */
42
    public function futureBookings(): HasMany
43
    {
44
        return $this->bookings()
45
                    ->whereNull('cancelled_at')
46
                    ->whereNotNull('starts_at')
47
                    ->where('starts_at', '>', Carbon::now());
48
    }
49
50
    /**
51
     * Get current bookings.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
54
     */
55
    public function currentBookings(): HasMany
56
    {
57
        return $this->bookings()
58
                    ->whereNull('cancelled_at')
59
                    ->whereNotNull('starts_at')
60
                    ->whereNotNull('ends_at')
61
                    ->where('starts_at', '<', Carbon::now())
62
                    ->where('ends_at', '>', Carbon::now());
63
    }
64
65
    /**
66
     * Get cancelled bookings.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
69
     */
70
    public function cancelledBookings(): HasMany
71
    {
72
        return $this->bookings()
73
                    ->whereNotNull('cancelled_at');
74
    }
75
76
    /**
77
     * Get bookings starts before the given date.
78
     *
79
     * @param string $date
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
82
     */
83
    public function bookingsStartsBefore(string $date): HasMany
84
    {
85
        return $this->bookings()
86
                    ->whereNull('cancelled_at')
87
                    ->whereNotNull('starts_at')
88
                    ->where('starts_at', '<', new Carbon($date));
89
    }
90
91
    /**
92
     * Get bookings starts after the given date.
93
     *
94
     * @param string $date
95
     *
96
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
97
     */
98
    public function bookingsStartsAfter(string $date): HasMany
99
    {
100
        return $this->bookings()
101
                    ->whereNull('cancelled_at')
102
                    ->whereNotNull('starts_at')
103
                    ->where('starts_at', '>', new Carbon($date));
104
    }
105
106
    /**
107
     * Get bookings starts between the given dates.
108
     *
109
     * @param string $starts
110
     * @param string $ends
111
     *
112
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
113
     */
114
    public function bookingsStartsBetween(string $starts, string $ends): HasMany
115
    {
116
        return $this->bookings()
117
                    ->whereNull('cancelled_at')
118
                    ->whereNotNull('starts_at')
119
                    ->where('starts_at', '>', new Carbon($starts))
120
                    ->where('starts_at', '<', new Carbon($ends));
121
    }
122
123
    /**
124
     * Get bookings ends before the given date.
125
     *
126
     * @param string $date
127
     *
128
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
129
     */
130
    public function bookingsEndsBefore(string $date): HasMany
131
    {
132
        return $this->bookings()
133
                    ->whereNull('cancelled_at')
134
                    ->whereNotNull('ends_at')
135
                    ->where('ends_at', '<', new Carbon($date));
136
    }
137
138
    /**
139
     * Get bookings ends after the given date.
140
     *
141
     * @param string $date
142
     *
143
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
144
     */
145
    public function bookingsEndsAfter(string $date): HasMany
146
    {
147
        return $this->bookings()
148
                    ->whereNull('cancelled_at')
149
                    ->whereNotNull('ends_at')
150
                    ->where('ends_at', '>', new Carbon($date));
151
    }
152
153
    /**
154
     * Get bookings ends between the given dates.
155
     *
156
     * @param string $starts
157
     * @param string $ends
158
     *
159
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
160
     */
161
    public function bookingsEndsBetween(string $starts, string $ends): HasMany
162
    {
163
        return $this->bookings()
164
                    ->whereNull('cancelled_at')
165
                    ->whereNotNull('ends_at')
166
                    ->where('ends_at', '>', new Carbon($starts))
167
                    ->where('ends_at', '<', new Carbon($ends));
168
    }
169
170
    /**
171
     * Get bookings cancelled before the given date.
172
     *
173
     * @param string $date
174
     *
175
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
176
     */
177
    public function bookingsCancelledBefore(string $date): HasMany
178
    {
179
        return $this->bookings()
180
                    ->whereNotNull('cancelled_at')
181
                    ->where('cancelled_at', '<', new Carbon($date));
182
    }
183
184
    /**
185
     * Get bookings cancelled after the given date.
186
     *
187
     * @param string $date
188
     *
189
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
190
     */
191
    public function bookingsCancelledAfter(string $date): HasMany
192
    {
193
        return $this->bookings()
194
                    ->whereNotNull('cancelled_at')
195
                    ->where('cancelled_at', '>', new Carbon($date));
196
    }
197
198
    /**
199
     * Get bookings cancelled between the given dates.
200
     *
201
     * @param string $starts
202
     * @param string $ends
203
     *
204
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
205
     */
206
    public function bookingsCancelledBetween(string $starts, string $ends): HasMany
207
    {
208
        return $this->bookings()
209
                    ->whereNotNull('cancelled_at')
210
                    ->where('cancelled_at', '>', new Carbon($starts))
211
                    ->where('cancelled_at', '<', new Carbon($ends));
212
    }
213
214
    /**
215
     * Get bookings of the given model.
216
     *
217
     * @param string $bookable
218
     *
219
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
220
     */
221
    public function bookingsOf(string $bookable): HasMany
222
    {
223
        return $this->bookings()
224
                    ->where('bookable_type', $bookable);
225
    }
226
227
    /**
228
     * Check if the person booked the given model.
229
     *
230
     * @param \Illuminate\Database\Eloquent\Model $model
231
     *
232
     * @return bool
233
     */
234
    public function isBooked(Model $model): bool
235
    {
236
        return $this->bookings()->where('bookable_id', $model->getKey())->where('bookable_type', get_class($model))->exists();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
237
    }
238
239
    /**
240
     * Book the given model at the given dates with the given price.
241
     *
242
     * @param \Illuminate\Database\Eloquent\Model $bookable
243
     * @param string                              $starts
244
     * @param string                              $ends
245
     * @param float                               $price
246
     *
247
     * @return \Rinvex\Bookings\Models\Booking
248
     */
249
    public function newBooking(Model $bookable, string $starts, string $ends, float $price): Booking
250
    {
251
        return $this->bookings()->create([
252
            'bookable_id' => $bookable->getKey(),
253
            'bookable_type' => get_class($bookable),
254
            'user_id' => $this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
255
            'starts_at' => $starts,
256
            'ends_at' => $ends,
257
            'price' => $price,
258
        ]);
259
    }
260
}
261