Bookable::activate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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