Completed
Push — master ( 3b0033...7c8bbb )
by Abdelrahman
05:32 queued 10s
created

Bookable::prices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Bookings\Traits;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Bookings\Models\BookableBooking;
9
use Illuminate\Database\Eloquent\Relations\MorphMany;
10
11
trait Bookable
12
{
13
    use BookingScopes;
14
15
    /**
16
     * Register a saved model event with the dispatcher.
17
     *
18
     * @param \Closure|string $callback
19
     *
20
     * @return void
21
     */
22
    abstract public static function saved($callback);
23
24
    /**
25
     * Register a deleted model event with the dispatcher.
26
     *
27
     * @param \Closure|string $callback
28
     *
29
     * @return void
30
     */
31
    abstract public static function deleted($callback);
32
33
    /**
34
     * Define a polymorphic one-to-many relationship.
35
     *
36
     * @param string $related
37
     * @param string $name
38
     * @param string $type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     * @param string $id
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * @param string $localKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $localKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
43
     */
44
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
45
46
    /**
47
     * Get the booking model name.
48
     *
49
     * @return string
50
     */
51
    abstract public static function getBookingModel(): string;
52
53
    /**
54
     * Get the rate model name.
55
     *
56
     * @return string
57
     */
58
    abstract public static function getRateModel(): string;
59
60
    /**
61
     * Get the availability model name.
62
     *
63
     * @return string
64
     */
65
    abstract public static function getAvailabilityModel(): string;
66
67
    /**
68
     * Boot the Bookable trait for the model.
69
     *
70
     * @return void
71
     */
72
    public static function bootBookable()
73
    {
74
        static::deleted(function (self $model) {
75
            $model->bookings()->delete();
76
        });
77
    }
78
79
    /**
80
     * Attach the given bookings to the model.
81
     *
82
     * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids
0 ignored issues
show
Bug introduced by
There is no parameter named $ids. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
83
     * @param mixed                                                                         $bookings
84
     *
85
     * @return void
86
     */
87
    public function setBookingsAttribute($bookings): void
88
    {
89
        static::saved(function (self $model) use ($bookings) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
            $this->bookings()->sync($bookings);
91
        });
92
    }
93
94
    /**
95
     * Attach the given rates to the model.
96
     *
97
     * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids
0 ignored issues
show
Bug introduced by
There is no parameter named $ids. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
98
     * @param mixed                                                                         $rates
99
     *
100
     * @return void
101
     */
102
    public function setRatesAttribute($rates): void
103
    {
104
        static::saved(function (self $model) use ($rates) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
105
            $this->rates()->sync($rates);
106
        });
107
    }
108
109
    /**
110
     * Attach the given availabilities to the model.
111
     *
112
     * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids
0 ignored issues
show
Bug introduced by
There is no parameter named $ids. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
113
     * @param mixed                                                                         $availabilities
114
     *
115
     * @return void
116
     */
117
    public function setAvailabilitiesAttribute($availabilities): void
118
    {
119
        static::saved(function (self $model) use ($availabilities) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
            $this->availabilities()->sync($availabilities);
121
        });
122
    }
123
124
    /**
125
     * The resource may have many bookings.
126
     *
127
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
128
     */
129
    public function bookings(): MorphMany
130
    {
131
        return $this->morphMany(static::getBookingModel(), 'bookable');
132
    }
133
134
    /**
135
     * Get bookings by the given customer.
136
     *
137
     * @param \Illuminate\Database\Eloquent\Model $customer
138
     *
139
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
140
     */
141
    public function bookingsBy(Model $customer): MorphMany
142
    {
143
        return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 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...
144
    }
145
146
    /**
147
     * The resource may have many availabilities.
148
     *
149
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
150
     */
151
    public function availabilities(): MorphMany
152
    {
153
        return $this->morphMany(static::getAvailabilityModel(), 'bookable');
154
    }
155
156
    /**
157
     * The resource may have many rates.
158
     *
159
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
160
     */
161
    public function rates(): MorphMany
162
    {
163
        return $this->morphMany(static::getRateModel(), 'bookable');
164
    }
165
166
    /**
167
     * Book the model for the given customer at the given dates with the given price.
168
     *
169
     * @param \Illuminate\Database\Eloquent\Model $customer
170
     * @param string                              $startsAt
171
     * @param string                              $endsAt
172
     *
173
     * @return \Rinvex\Bookings\Models\BookableBooking
174
     */
175
    public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking
176
    {
177
        return $this->bookings()->create([
178
            'bookable_id' => static::getKey(),
179
            'bookable_type' => static::getMorphClass(),
180
            'customer_id' => $customer->getKey(),
181
            'customer_type' => $customer->getMorphClass(),
182
            'starts_at' => (new Carbon($startsAt))->toDateTimeString(),
183
            'ends_at' => (new Carbon($endsAt))->toDateTimeString(),
184
        ]);
185
    }
186
}
187