Completed
Push — develop ( 7b3847...0c6ec4 )
by Abdelrahman
03:10
created

Bookable::setAddonsAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

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 3
nc 1
nop 1
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
     *
84
     * @return void
85
     */
86
    public function setBookingsAttribute($bookings): void
87
    {
88
        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...
89
            $this->bookings()->sync($bookings);
90
        });
91
    }
92
93
    /**
94
     * Attach the given rates to the model.
95
     *
96
     * @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...
97
     *
98
     * @return void
99
     */
100
    public function setRatesAttribute($rates): void
101
    {
102
        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...
103
            $this->rates()->sync($rates);
104
        });
105
    }
106
107
    /**
108
     * Attach the given availabilities to the model.
109
     *
110
     * @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...
111
     *
112
     * @return void
113
     */
114
    public function setAvailabilitiesAttribute($availabilities): void
115
    {
116
        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...
117
            $this->availabilities()->sync($availabilities);
118
        });
119
    }
120
121
    /**
122
     * The resource may have many bookings.
123
     *
124
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
125
     */
126
    public function bookings(): MorphMany
127
    {
128
        return $this->morphMany(static::getBookingModel(), 'bookable');
129
    }
130
131
    /**
132
     * Get bookings by the given customer.
133
     *
134
     * @param \Illuminate\Database\Eloquent\Model $customer
135
     *
136
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
137
     */
138
    public function bookingsBy(Model $customer): MorphMany
139
    {
140
        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...
141
    }
142
143
    /**
144
     * The resource may have many availabilities.
145
     *
146
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
147
     */
148
    public function availabilities(): MorphMany
149
    {
150
        return $this->morphMany(static::getAvailabilityModel(), 'bookable');
151
    }
152
153
    /**
154
     * The resource may have many rates.
155
     *
156
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
157
     */
158
    public function rates(): MorphMany
159
    {
160
        return $this->morphMany(static::getRateModel(), 'bookable');
161
    }
162
163
    /**
164
     * Book the model for the given customer at the given dates with the given price.
165
     *
166
     * @param \Illuminate\Database\Eloquent\Model $customer
167
     * @param string                              $startsAt
168
     * @param string                              $endsAt
169
     *
170
     * @return \Rinvex\Bookings\Models\BookableBooking
171
     */
172
    public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking
173
    {
174
        return $this->bookings()->create([
175
            'bookable_id' => static::getKey(),
176
            'bookable_type' => static::getMorphClass(),
177
            'customer_id' => $customer->getKey(),
178
            'customer_type' => $customer->getMorphClass(),
179
            'starts_at' => (new Carbon($startsAt))->toDateTimeString(),
180
            'ends_at' => (new Carbon($endsAt))->toDateTimeString(),
181
        ]);
182
    }
183
}
184