Booking::getTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace IproSync\Models;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Casts\Attribute;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, IproSync\Models\Attribute. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Support\Str;
11
use IproSync\Database\Factories\BookingFactory;
12
use IproSync\Ipro\Price;
13
14
class Booking extends Model
15
{
16
    use HasFactory;
17
    use HasTraitsWithCasts, HasPullAt;
18
19
    public $incrementing = false;
20
21
    protected $guarded = [];
22
23
    protected $casts = [
24
        'order_time'             => 'datetime:Y-m-d',
25
        'modified_time'          => 'datetime',
26
        'check_in'               => 'datetime:Y-m-d',
27
        'check_out'              => 'datetime:Y-m-d',
28
        'deposit_due_date'       => 'datetime:Y-m-d',
29
        'balance_due_date'       => 'datetime:Y-m-d',
30
        'property_types'         => 'array',
31
        'booking_tags'           => 'array',
32
        'guests'                 => 'array',
33
        'holiday_extras_ordered' => 'array',
34
        'payment_schedules'      => 'array',
35
        'payments'               => 'array',
36
        'bills'                  => 'array',
37
        'renter_amount'          => 'float',
38
        'holiday_extras'         => 'float',
39
        'discount'               => 'float',
40
    ];
41
42 3
    public function getTable(): string
43
    {
44 3
        return config('iprosoftware-sync.tables.bookings');
45
    }
46
47 3
    protected static function newFactory(): BookingFactory
48
    {
49 3
        return BookingFactory::new();
50
    }
51
52 1
    public function name(): Attribute
53
    {
54 1
        return Attribute::get(fn () => implode(' - ', array_filter([
55 1
            $this->check_in?->format(config('iprosoftware-sync.date_format.display')),
0 ignored issues
show
Bug introduced by
The property check_in does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
56 1
            $this->check_out?->format(config('iprosoftware-sync.date_format.display')),
0 ignored issues
show
Bug introduced by
The property check_out does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
57 1
        ])) ?: '-');
58
    }
59
60 1
    public function renterRawTotal(): Attribute
61
    {
62 1
        return Attribute::get(fn () => round($this->renter_amount + $this->holiday_extras - $this->discount, 2));
0 ignored issues
show
Bug introduced by
The property discount does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property renter_amount does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property holiday_extras does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
63
    }
64
65
    public function property(): BelongsTo
66
    {
67
        return $this->belongsTo(Property::class, 'property_id', 'id');
68
    }
69
70
    public function contact(): BelongsTo
71
    {
72
        return $this->belongsTo(Contact::class, 'contact_id', 'id');
73
    }
74
75
    public function scopeActive(Builder $query)
76
    {
77
        $query->where('booking_status_id', 3);
78
    }
79
80 1
    public function formattedPrice(string $attributeName): string
81
    {
82 1
        return Price::format((float)$this->$attributeName, $this->currency);
0 ignored issues
show
Bug introduced by
The property currency does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
83
    }
84
85 1
    public function extractGuestNote(string $key, ?string $default = null, array $separators = [
86
        ':',
87
        ' - ',
88
    ]): ?string
89
    {
90 1
        $key  = rtrim($key);
91 1
        $keys = [];
92 1
        foreach ($separators as $separator) {
93 1
            $keys[] = $key . $separator;
94
        }
95 1
        foreach (preg_split("/((\r?\n)|(\r\n?))/", $this->guest_notes ?? '') as $note) {
0 ignored issues
show
Bug introduced by
The property guest_notes does not seem to exist on IproSync\Models\Booking. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
96 1
            $note = trim($note);
97 1
            foreach ($keys as $formattedKey) {
98 1
                if (Str::startsWith($note, $formattedKey)) {
99 1
                    return trim(Str::after($note, $formattedKey));
100
                }
101
            }
102
        }
103
104 1
        return $default;
105
    }
106
}
107