Completed
Push — develop ( fa8a7a...dedc5f )
by Abdelrahman
01:28
created

Bookable::activate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
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\Models;
6
7
use Spatie\Sluggable\SlugOptions;
8
use Rinvex\Support\Traits\HasSlug;
9
use Spatie\EloquentSortable\Sortable;
10
use Illuminate\Database\Eloquent\Model;
11
use Rinvex\Cacheable\CacheableEloquent;
12
use Illuminate\Database\Eloquent\Builder;
13
use Rinvex\Support\Traits\HasTranslations;
14
use Rinvex\Support\Traits\ValidatingTrait;
15
use Spatie\EloquentSortable\SortableTrait;
16
use Rinvex\Bookings\Traits\Bookable as BookableTrait;
17
18
abstract class Bookable extends Model implements Sortable
19
{
20
    use HasSlug;
21
    use BookableTrait;
22
    use SortableTrait;
23
    use HasTranslations;
24
    use ValidatingTrait;
25
    use CacheableEloquent;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $fillable = [
31
        'name',
32
        'title',
33
        'description',
34
        'is_active',
35
        'price',
36
        'unit',
37
        'currency',
38
        'style',
39
        'sort_order',
40
        'capacity',
41
        'early_booking_limit',
42
        'late_booking_limit',
43
        'late_cancellation_limit',
44
        'maximum_booking_length',
45
        'minimum_booking_length',
46
        'booking_interval_limit',
47
    ];
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected $casts = [
53
        'name' => 'string',
54
        'title' => 'string',
55
        'description' => 'string',
56
        'is_active' => 'boolean',
57
        'price' => 'number',
58
        'unit' => 'string',
59
        'currency' => 'string',
60
        'style' => 'string',
61
        'sort_order' => 'integer',
62
        'capacity' => 'integer',
63
        'early_booking_limit' => 'integer',
64
        'late_booking_limit' => 'integer',
65
        'late_cancellation_limit' => 'integer',
66
        'maximum_booking_length' => 'integer',
67
        'minimum_booking_length' => 'integer',
68
        'booking_interval_limit' => 'integer',
69
        'deleted_at' => 'datetime',
70
    ];
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected $observables = [
76
        'validating',
77
        'validated',
78
    ];
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public $translatable = [
84
        'title',
85
        'description',
86
    ];
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public $sortable = [
92
        'order_column_name' => 'sort_order',
93
    ];
94
95
    /**
96
     * The default rules that the model will validate against.
97
     *
98
     * @var array
99
     */
100
    protected $rules = [];
101
102
    /**
103
     * Get the active resources.
104
     *
105
     * @param \Illuminate\Database\Eloquent\Builder $builder
106
     *
107
     * @return \Illuminate\Database\Eloquent\Builder
108
     */
109
    public function scopeActive(Builder $builder): Builder
110
    {
111
        return $builder->where('is_active', true);
112
    }
113
114
    /**
115
     * Get the inactive resources.
116
     *
117
     * @param \Illuminate\Database\Eloquent\Builder $builder
118
     *
119
     * @return \Illuminate\Database\Eloquent\Builder
120
     */
121
    public function scopeInactive(Builder $builder): Builder
122
    {
123
        return $builder->where('is_active', false);
124
    }
125
126
    /**
127
     * Get the options for generating the slug.
128
     *
129
     * @return \Spatie\Sluggable\SlugOptions
130
     */
131
    public function getSlugOptions(): SlugOptions
132
    {
133
        return SlugOptions::create()
134
                          ->doNotGenerateSlugsOnUpdate()
135
                          ->generateSlugsFrom('title')
136
                          ->saveSlugsTo('name');
137
    }
138
139
    /**
140
     * Activate the resource.
141
     *
142
     * @return $this
143
     */
144
    public function activate()
145
    {
146
        $this->update(['is_active' => true]);
147
148
        return $this;
149
    }
150
151
    /**
152
     * Deactivate the resource.
153
     *
154
     * @return $this
155
     */
156
    public function deactivate()
157
    {
158
        $this->update(['is_active' => false]);
159
160
        return $this;
161
    }
162
}
163