Completed
Push — develop ( a73468...4b8880 )
by Abdelrahman
01:11
created

Bookable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 7
dl 0
loc 170
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A scopeActive() 0 4 1
A scopeInactive() 0 4 1
A getSlugOptions() 0 7 1
A activate() 0 6 1
A deactivate() 0 6 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 Illuminate\Database\Eloquent\Builder;
12
use Rinvex\Support\Traits\HasTranslations;
13
use Rinvex\Support\Traits\ValidatingTrait;
14
use Spatie\EloquentSortable\SortableTrait;
15
use Rinvex\Bookings\Traits\Bookable as BookableTrait;
16
17
abstract class Bookable extends Model implements Sortable
18
{
19
    use HasSlug;
20
    use BookableTrait;
21
    use SortableTrait;
22
    use HasTranslations;
23
    use ValidatingTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected $fillable = [
29
        'slug',
30
        'name',
31
        'description',
32
        'is_active',
33
        'base_cost',
34
        'unit_cost',
35
        'currency',
36
        'unit',
37
        'maximum_units',
38
        'minimum_units',
39
        'is_cancelable',
40
        'is_recurring',
41
        'sort_order',
42
        'capacity',
43
        'style',
44
    ];
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected $casts = [
50
        'slug' => 'string',
51
        'name' => 'string',
52
        'description' => 'string',
53
        'is_active' => 'boolean',
54
        'base_cost' => 'float',
55
        'unit_cost' => 'float',
56
        'currency' => 'string',
57
        'unit' => 'string',
58
        'maximum_units' => 'integer',
59
        'minimum_units' => 'integer',
60
        'is_cancelable' => 'boolean',
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
     * Create a new Eloquent model instance.
100
     *
101
     * @param array $attributes
102
     */
103
    public function __construct(array $attributes = [])
104
    {
105
        parent::__construct($attributes);
106
107
        $this->setRules([
108
            'slug' => 'required|alpha_dash|max:150',
109
            'name' => 'required|string|strip_tags|max:150',
110
            'description' => 'nullable|string|max:32768',
111
            'is_active' => 'sometimes|boolean',
112
            'base_cost' => 'required|numeric',
113
            'unit_cost' => 'required|numeric',
114
            'currency' => 'required|string|size:3',
115
            'unit' => 'required|in:minute,hour,day,month',
116
            'maximum_units' => 'nullable|integer|max:100000',
117
            'minimum_units' => 'nullable|integer|max:100000',
118
            'is_cancelable' => 'nullable|boolean',
119
            'is_recurring' => 'nullable|boolean',
120
            'sort_order' => 'nullable|integer|max:100000',
121
            'capacity' => 'nullable|integer|max:100000',
122
            'style' => 'nullable|string|strip_tags|max:150',
123
        ]);
124
    }
125
126
    /**
127
     * Get the active resources.
128
     *
129
     * @param \Illuminate\Database\Eloquent\Builder $builder
130
     *
131
     * @return \Illuminate\Database\Eloquent\Builder
132
     */
133
    public function scopeActive(Builder $builder): Builder
134
    {
135
        return $builder->where('is_active', true);
136
    }
137
138
    /**
139
     * Get the inactive resources.
140
     *
141
     * @param \Illuminate\Database\Eloquent\Builder $builder
142
     *
143
     * @return \Illuminate\Database\Eloquent\Builder
144
     */
145
    public function scopeInactive(Builder $builder): Builder
146
    {
147
        return $builder->where('is_active', false);
148
    }
149
150
    /**
151
     * Get the options for generating the slug.
152
     *
153
     * @return \Spatie\Sluggable\SlugOptions
154
     */
155
    public function getSlugOptions(): SlugOptions
156
    {
157
        return SlugOptions::create()
158
                          ->doNotGenerateSlugsOnUpdate()
159
                          ->generateSlugsFrom('name')
160
                          ->saveSlugsTo('slug');
161
    }
162
163
    /**
164
     * Activate the resource.
165
     *
166
     * @return $this
167
     */
168
    public function activate()
169
    {
170
        $this->update(['is_active' => true]);
171
172
        return $this;
173
    }
174
175
    /**
176
     * Deactivate the resource.
177
     *
178
     * @return $this
179
     */
180
    public function deactivate()
181
    {
182
        $this->update(['is_active' => false]);
183
184
        return $this;
185
    }
186
}
187