Completed
Push — develop ( 973520...768b39 )
by Abdelrahman
01:27
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\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
     * The resource may have many bookings.
17
     *
18
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
19
     */
20
    public function bookings(): MorphMany
21
    {
22
        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...
23
    }
24
25
    /**
26
     * Get bookings of the given customer.
27
     *
28
     * @param \Illuminate\Database\Eloquent\Model $customer
29
     *
30
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
31
     */
32
    public function bookingsOf(Model $customer): MorphMany
33
    {
34
        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...
35
    }
36
37
    /**
38
     * The resource may have many addons.
39
     *
40
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
41
     */
42
    public function addons(): MorphMany
43
    {
44
        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...
45
    }
46
47
    /**
48
     * The resource may have many availabilities.
49
     *
50
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
51
     */
52
    public function availabilities(): MorphMany
53
    {
54
        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...
55
    }
56
57
    /**
58
     * The resource may have many rates.
59
     *
60
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
61
     */
62
    public function rates(): MorphMany
63
    {
64
        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...
65
    }
66
67
    /**
68
     * Book the model for the given customer at the given dates with the given price.
69
     *
70
     * @param \Illuminate\Database\Eloquent\Model $customer
71
     * @param string                              $startsAt
72
     * @param string                              $endsAt
73
     *
74
     * @return \Rinvex\Bookings\Models\Booking
75
     */
76
    public function newBooking(Model $customer, string $startsAt, string $endsAt): Booking
77
    {
78
        return $this->bookings()->create([
79
            'bookable_id' => static::getKey(),
80
            'bookable_type' => static::getMorphClass(),
81
            'customer_id' => $customer->getKey(),
82
            'customer_type' => $customer->getMorphClass(),
83
            'starts_at' => (new Carbon($startsAt))->toDateTimeString(),
84
            'ends_at' => (new Carbon($endsAt))->toDateTimeString(),
85
        ]);
86
    }
87
}
88