Completed
Push — develop ( 3de1d7...233a23 )
by Abdelrahman
01:42
created

TicketableBooking::makeActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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 Illuminate\Database\Eloquent\Model;
9
use Rinvex\Cacheable\CacheableEloquent;
10
use Illuminate\Database\Eloquent\Builder;
11
use Rinvex\Support\Traits\ValidatingTrait;
12
13
class TicketableBooking extends Model
14
{
15
    use ValidatingTrait;
16
    use CacheableEloquent;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $fillable = [
22
        'ticket_id',
23
        'customer_id',
24
        'paid',
25
        'currency',
26
        'notes',
27
    ];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected $casts = [
33
        'ticket_id' => 'integer',
34
        'customer_id' => 'integer',
35
        'paid' => 'float',
36
        'currency' => 'string',
37
        'notes' => 'string',
38
        'deleted_at' => 'datetime',
39
    ];
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected $observables = [
45
        'validating',
46
        'validated',
47
    ];
48
49
    /**
50
     * The default rules that the model will validate against.
51
     *
52
     * @var array
53
     */
54
    protected $rules = [
55
        'ticket_id' => 'required|integer',
56
        'customer_id' => 'required|integer',
57
        'paid' => 'required|numeric',
58
        'currency' => 'required|alpha|size:3',
59
        'notes' => 'nullable|string|max:10000',
60
    ];
61
62
    /**
63
     * Whether the model should throw a
64
     * ValidationException if it fails validation.
65
     *
66
     * @var bool
67
     */
68
    protected $throwValidationExceptions = true;
69
70
    /**
71
     * Create a new Eloquent model instance.
72
     *
73
     * @param array $attributes
74
     */
75
    public function __construct(array $attributes = [])
76
    {
77
        parent::__construct($attributes);
78
79
        $this->setTable(config('rinvex.bookings.tables.ticketable_bookings'));
80
    }
81
82
    /**
83
     * Get the active resources.
84
     *
85
     * @param \Illuminate\Database\Eloquent\Builder $builder
86
     *
87
     * @return \Illuminate\Database\Eloquent\Builder
88
     */
89
    public function scopeActive(Builder $builder): Builder
90
    {
91
        return $builder->where('is_active', true);
92
    }
93
94
    /**
95
     * Get the inactive resources.
96
     *
97
     * @param \Illuminate\Database\Eloquent\Builder $builder
98
     *
99
     * @return \Illuminate\Database\Eloquent\Builder
100
     */
101
    public function scopeInactive(Builder $builder): Builder
102
    {
103
        return $builder->where('is_active', false);
104
    }
105
106
    /**
107
     * Get the options for generating the slug.
108
     *
109
     * @return \Spatie\Sluggable\SlugOptions
110
     */
111
    public function getSlugOptions(): SlugOptions
112
    {
113
        return SlugOptions::create()
114
                          ->doNotGenerateSlugsOnUpdate()
115
                          ->generateSlugsFrom('name')
116
                          ->saveSlugsTo('slug');
117
    }
118
119
    /**
120
     * Activate the resource.
121
     *
122
     * @return $this
123
     */
124
    public function makeActive()
125
    {
126
        $this->update(['is_active' => true]);
127
128
        return $this;
129
    }
130
131
    /**
132
     * Deactivate the resource.
133
     *
134
     * @return $this
135
     */
136
    public function makeInactive()
137
    {
138
        $this->update(['is_active' => false]);
139
140
        return $this;
141
    }
142
}
143