Completed
Push — master ( 75befc...3b0033 )
by Abdelrahman
06:46 queued 05:33
created

Bookable::newPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Bookings\Traits;
6
7
use Rinvex\Bookings\Models\Rate;
8
use Rinvex\Bookings\Models\Price;
9
use Rinvex\Bookings\Models\Booking;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\MorphMany;
12
13
trait Bookable
14
{
15
    use BookingScopes;
16
17
    /**
18
     * The resource may have many bookings.
19
     *
20
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
21
     */
22
    public function bookings(): MorphMany
23
    {
24
        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...
25
    }
26
27
    /**
28
     * Get bookings of the given user.
29
     *
30
     * @param \Illuminate\Database\Eloquent\Model $user
31
     *
32
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
33
     */
34
    public function bookingsOf(Model $user): MorphMany
35
    {
36
        return $this->bookings()->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey());
37
    }
38
39
    /**
40
     * The resource may have many rates.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
43
     */
44
    public function rates(): MorphMany
45
    {
46
        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...
47
    }
48
49
    /**
50
     * The resource may have many prices.
51
     *
52
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
53
     */
54
    public function prices(): MorphMany
55
    {
56
        return $this->morphMany(config('rinvex.bookings.models.price'), '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
     * Book the model for the given user at the given dates with the given price.
61
     *
62
     * @param \Illuminate\Database\Eloquent\Model $user
63
     * @param string                              $startsAt
64
     * @param string                              $endsAt
65
     *
66
     * @return \Rinvex\Bookings\Models\Booking
67
     */
68
    public function newBooking(Model $user, string $startsAt, string $endsAt): Booking
69
    {
70
        return $this->bookings()->create([
71
            'bookable_id' => static::getKey(),
72
            'bookable_type' => static::getMorphClass(),
73
            'user_id' => $user->getKey(),
74
            'user_type' => $user->getMorphClass(),
75
            'starts_at' => (new Carbon($startsAt))->toDateTimeString(),
76
            'ends_at' => (new Carbon($endsAt))->toDateTimeString(),
77
        ]);
78
    }
79
80
    /**
81
     * Create a new booking rate.
82
     *
83
     * @param float  $percentage
84
     * @param string $operator
85
     * @param int    $amount
86
     *
87
     * @return \Rinvex\Bookings\Models\Rate
88
     */
89
    public function newRate(float $percentage, string $operator, int $amount): Rate
90
    {
91
        return $this->rates()->create([
92
            'bookable_id' => static::getKey(),
93
            'bookable_type' => static::getMorphClass(),
94
            'percentage' => $percentage,
95
            'operator' => $operator,
96
            'amount' => $amount,
97
        ]);
98
    }
99
100
    /**
101
     * Create a new booking price.
102
     *
103
     * @param string $weekday
104
     * @param string $startsAt
105
     * @param string $endsAt
106
     * @param float  $percentage
107
     *
108
     * @return \Rinvex\Bookings\Models\Price
109
     */
110
    public function newPrice(string $weekday, string $startsAt, string $endsAt, float $percentage): Price
111
    {
112
        return $this->prices()->create([
113
            'bookable_id' => static::getKey(),
114
            'bookable_type' => static::getMorphClass(),
115
            'percentage' => $percentage,
116
            'weekday' => $weekday,
117
            'starts_at' => (new Carbon($startsAt))->toTimeString(),
118
            'ends_at' => (new Carbon($endsAt))->toTimeString(),
119
        ]);
120
    }
121
}
122