Completed
Push — develop ( bc580e...3de1d7 )
by Abdelrahman
01:25
created

Bookable::bootBookable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Bookings\Traits;
6
7
use Rinvex\Bookings\Models\Booking;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\MorphMany;
10
11
trait Bookable
12
{
13
    use BookingScopes;
14
15
    /**
16
     * Boot the Bookable trait for the model.
17
     *
18
     * @return void
19
     */
20
    public static function bootBookable()
21
    {
22
        static::deleted(function (self $model) {
23
            $model->bookings()->delete();
24
        });
25
    }
26
27
    /**
28
     * The resource may have many bookings.
29
     *
30
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
31
     */
32
    public function bookings(): MorphMany
33
    {
34
        return $this->morphMany(config('rinvex.bookings.models.booking'), 'bookable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
35
    }
36
37
    /**
38
     * Get bookings of the given customer.
39
     *
40
     * @param \Illuminate\Database\Eloquent\Model $customer
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
43
     */
44
    public function bookingsOf(Model $customer): MorphMany
45
    {
46
        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...
47
    }
48
49
    /**
50
     * The resource may have many addons.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
53
     */
54
    public function addons(): MorphMany
55
    {
56
        return $this->morphMany(config('rinvex.bookings.models.addon'), 'bookable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
57
    }
58
59
    /**
60
     * The resource may have many availabilities.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
63
     */
64
    public function availabilities(): MorphMany
65
    {
66
        return $this->morphMany(config('rinvex.bookings.models.availability'), 'bookable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
67
    }
68
69
    /**
70
     * The resource may have many rates.
71
     *
72
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
73
     */
74
    public function rates(): MorphMany
75
    {
76
        return $this->morphMany(config('rinvex.bookings.models.rate'), 'bookable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
77
    }
78
79
    /**
80
     * Book the model for the given customer at the given dates with the given price.
81
     *
82
     * @param \Illuminate\Database\Eloquent\Model $customer
83
     * @param string                              $startsAt
84
     * @param string                              $endsAt
85
     *
86
     * @return \Rinvex\Bookings\Models\Booking
87
     */
88
    public function newBooking(Model $customer, string $startsAt, string $endsAt): Booking
89
    {
90
        return $this->bookings()->create([
91
            'bookable_id' => static::getKey(),
92
            'bookable_type' => static::getMorphClass(),
93
            'customer_id' => $customer->getKey(),
94
            'customer_type' => $customer->getMorphClass(),
95
            'starts_at' => (new Carbon($startsAt))->toDateTimeString(),
96
            'ends_at' => (new Carbon($endsAt))->toDateTimeString(),
97
        ]);
98
    }
99
}
100