TicketableTicket::scopeActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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 Illuminate\Database\Eloquent\Relations\MorphTo;
17
18
class TicketableTicket extends Model implements Sortable
19
{
20
    use HasSlug;
21
    use SortableTrait;
22
    use HasTranslations;
23
    use ValidatingTrait;
24
    use CacheableEloquent;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected $fillable = [
30
        'ticketable_id',
31
        'ticketable_type',
32
        'slug',
33
        'name',
34
        'description',
35
        'is_active',
36
        'price',
37
        'currency',
38
        'quantity',
39
        'sort_order',
40
    ];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected $casts = [
46
        'ticketable_id' => 'integer',
47
        'ticketable_type' => 'string',
48
        'slug' => 'string',
49
        'name' => 'string',
50
        'description' => 'string',
51
        'is_active' => 'boolean',
52
        'price' => 'float',
53
        'currency' => 'string',
54
        'quantity' => 'integer',
55
        'sort_order' => 'integer',
56
        'deleted_at' => 'datetime',
57
    ];
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected $observables = [
63
        'validating',
64
        'validated',
65
    ];
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public $translatable = [
71
        'name',
72
        'description',
73
    ];
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public $sortable = [
79
        'order_column_name' => 'sort_order',
80
    ];
81
82
    /**
83
     * The default rules that the model will validate against.
84
     *
85
     * @var array
86
     */
87
    protected $rules = [];
88
89
    /**
90
     * Whether the model should throw a
91
     * ValidationException if it fails validation.
92
     *
93
     * @var bool
94
     */
95
    protected $throwValidationExceptions = true;
96
97
    /**
98
     * Create a new Eloquent model instance.
99
     *
100
     * @param array $attributes
101
     */
102
    public function __construct(array $attributes = [])
103
    {
104
        parent::__construct($attributes);
105
106
        $this->setTable(config('rinvex.bookings.tables.ticketable_tickets'));
107
        $this->setRules([
108
            'ticketable_id' => 'required|integer',
109
            'ticketable_type' => 'required|string',
110
            'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.bookings.tables.ticketable_tickets').',slug,NULL,id,ticketable_id,'.($this->ticketable_id ?? 'null').',ticketable_type,'.($this->ticketable_type ?? 'null'),
111
            'name' => 'required|string|max:150',
112
            'description' => 'nullable|string|max:10000',
113
            'is_active' => 'sometimes|boolean',
114
            'price' => 'required|numeric',
115
            'currency' => 'required|alpha|size:3',
116
            'quantity' => 'nullable|integer|max:10000000',
117
            'sort_order' => 'nullable|integer|max:10000000',
118
        ]);
119
    }
120
121
    /**
122
     * Get the active resources.
123
     *
124
     * @param \Illuminate\Database\Eloquent\Builder $builder
125
     *
126
     * @return \Illuminate\Database\Eloquent\Builder
127
     */
128
    public function scopeActive(Builder $builder): Builder
129
    {
130
        return $builder->where('is_active', true);
131
    }
132
133
    /**
134
     * Get the inactive resources.
135
     *
136
     * @param \Illuminate\Database\Eloquent\Builder $builder
137
     *
138
     * @return \Illuminate\Database\Eloquent\Builder
139
     */
140
    public function scopeInactive(Builder $builder): Builder
141
    {
142
        return $builder->where('is_active', false);
143
    }
144
145
    /**
146
     * Get the options for generating the slug.
147
     *
148
     * @return \Spatie\Sluggable\SlugOptions
149
     */
150
    public function getSlugOptions(): SlugOptions
151
    {
152
        return SlugOptions::create()
153
                          ->doNotGenerateSlugsOnUpdate()
154
                          ->generateSlugsFrom('name')
155
                          ->saveSlugsTo('slug');
156
    }
157
158
    /**
159
     * Activate the resource.
160
     *
161
     * @return $this
162
     */
163
    public function activate()
164
    {
165
        $this->update(['is_active' => true]);
166
167
        return $this;
168
    }
169
170
    /**
171
     * Deactivate the resource.
172
     *
173
     * @return $this
174
     */
175
    public function deactivate()
176
    {
177
        $this->update(['is_active' => false]);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Get the owning resource model.
184
     *
185
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
186
     */
187
    public function ticketable(): MorphTo
188
    {
189
        return $this->morphTo('ticketable', 'ticketable_type', 'ticketable_id');
190
    }
191
}
192