Completed
Push — develop ( bc580e...3de1d7 )
by Abdelrahman
01:25
created

Bookable::scopeInactive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
        'slug',
32
        'name',
33
        'description',
34
        'is_active',
35
        'base_cost',
36
        'unit_cost',
37
        'currency',
38
        'unit',
39
        'maximum_units',
40
        'minimum_units',
41
        'is_recurring',
42
        'sort_order',
43
        'capacity',
44
        'style',
45
    ];
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected $casts = [
51
        'slug' => 'string',
52
        'name' => 'string',
53
        'description' => 'string',
54
        'is_active' => 'boolean',
55
        'base_cost' => 'float',
56
        'unit_cost' => 'float',
57
        'currency' => 'string',
58
        'unit' => 'string',
59
        'maximum_units' => 'integer',
60
        'minimum_units' => 'integer',
61
        'is_recurring' => 'boolean',
62
        'sort_order' => 'integer',
63
        'capacity' => 'integer',
64
        'style' => 'string',
65
        'deleted_at' => 'datetime',
66
    ];
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    protected $observables = [
72
        'validating',
73
        'validated',
74
    ];
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public $translatable = [
80
        'name',
81
        'description',
82
    ];
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public $sortable = [
88
        'order_column_name' => 'sort_order',
89
    ];
90
91
    /**
92
     * The default rules that the model will validate against.
93
     *
94
     * @var array
95
     */
96
    protected $rules = [];
97
98
    /**
99
     * Get the active resources.
100
     *
101
     * @param \Illuminate\Database\Eloquent\Builder $builder
102
     *
103
     * @return \Illuminate\Database\Eloquent\Builder
104
     */
105
    public function scopeActive(Builder $builder): Builder
106
    {
107
        return $builder->where('is_active', true);
108
    }
109
110
    /**
111
     * Get the inactive resources.
112
     *
113
     * @param \Illuminate\Database\Eloquent\Builder $builder
114
     *
115
     * @return \Illuminate\Database\Eloquent\Builder
116
     */
117
    public function scopeInactive(Builder $builder): Builder
118
    {
119
        return $builder->where('is_active', false);
120
    }
121
122
    /**
123
     * Get the options for generating the slug.
124
     *
125
     * @return \Spatie\Sluggable\SlugOptions
126
     */
127
    public function getSlugOptions(): SlugOptions
128
    {
129
        return SlugOptions::create()
130
                          ->doNotGenerateSlugsOnUpdate()
131
                          ->generateSlugsFrom('name')
132
                          ->saveSlugsTo('slug');
133
    }
134
135
    /**
136
     * Activate the resource.
137
     *
138
     * @return $this
139
     */
140
    public function makeActive()
141
    {
142
        $this->update(['is_active' => true]);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Deactivate the resource.
149
     *
150
     * @return $this
151
     */
152
    public function makeInactive()
153
    {
154
        $this->update(['is_active' => false]);
155
156
        return $this;
157
    }
158
}
159