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

HasBookings::bookings()   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 Rinvex\Bookings\Models\Booking;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\MorphMany;
10
11
trait HasBookings
12
{
13
    use BookingScopes;
14
15
    /**
16
     * Define a polymorphic one-to-many relationship.
17
     *
18
     * @param string $related
19
     * @param string $name
20
     * @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...
21
     * @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...
22
     * @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...
23
     *
24
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
25
     */
26
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
27
28
    /**
29
     * The user may have many bookings.
30
     *
31
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
32
     */
33
    public function bookings(): MorphMany
34
    {
35
        return $this->morphMany(config('rinvex.bookings.models.booking'), 'user');
36
    }
37
38
    /**
39
     * Get bookings of the given resource.
40
     *
41
     * @param \Illuminate\Database\Eloquent\Model $bookable
42
     *
43
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
44
     */
45
    public function bookingsOf(Model $bookable): MorphMany
46
    {
47
        return $this->bookings()->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->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...
48
    }
49
50
    /**
51
     * Check if the person booked the given model.
52
     *
53
     * @param \Illuminate\Database\Eloquent\Model $bookable
54
     *
55
     * @return bool
56
     */
57
    public function isBooked(Model $bookable): bool
58
    {
59
        return $this->bookings()->where('bookable_id', $bookable->getKey())->exists();
60
    }
61
62
    /**
63
     * Book the given model at the given dates with the given price.
64
     *
65
     * @param \Illuminate\Database\Eloquent\Model $bookable
66
     * @param string                              $startsAt
67
     * @param string                              $endsAt
68
     *
69
     * @return \Rinvex\Bookings\Models\Booking
70
     */
71
    public function newBooking(Model $bookable, string $startsAt, string $endsAt): Booking
72
    {
73
        return $this->bookings()->create([
74
            'bookable_id' => $bookable->getKey(),
75
            'bookable_type' => $bookable->getMorphClass(),
76
            'user_id' => $this->getKey(),
0 ignored issues
show
Bug introduced by
It seems like getKey() 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
            'user_type' => $this->getMorphClass(),
0 ignored issues
show
Bug introduced by
It seems like getMorphClass() 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...
78
            'starts_at' => $startsAt,
79
            'ends_at' => $endsAt,
80
        ]);
81
    }
82
}
83