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

Ticketable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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 Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Builder;
11
use Rinvex\Support\Traits\HasTranslations;
12
use Rinvex\Support\Traits\ValidatingTrait;
13
use Rinvex\Bookings\Traits\Ticketable as TicketableTrait;
14
15
abstract class Ticketable extends Model
16
{
17
    use HasSlug;
18
    use TicketableTrait;
19
    use HasTranslations;
20
    use ValidatingTrait;
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected $fillable = [
26
        'slug',
27
        'name',
28
        'description',
29
        'is_public',
30
        'starts_at',
31
        'ends_at',
32
        'timezone',
33
        'location',
34
    ];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected $casts = [
40
        'slug' => 'string',
41
        'name' => 'string',
42
        'description' => 'string',
43
        'is_public' => 'boolean',
44
        'starts_at' => 'datetime',
45
        'ends_at' => 'datetime',
46
        'timezone' => 'string',
47
        'location' => 'string',
48
        'deleted_at' => 'datetime',
49
    ];
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    protected $observables = [
55
        'validating',
56
        'validated',
57
    ];
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public $translatable = [
63
        'name',
64
        'description',
65
    ];
66
67
    /**
68
     * The default rules that the model will validate against.
69
     *
70
     * @var array
71
     */
72
    protected $rules = [];
73
74
    /**
75
     * Whether the model should throw a
76
     * ValidationException if it fails validation.
77
     *
78
     * @var bool
79
     */
80
    protected $throwValidationExceptions = true;
81
82
    /**
83
     * Create a new Eloquent model instance.
84
     *
85
     * @param array $attributes
86
     */
87
    public function __construct(array $attributes = [])
88
    {
89
        parent::__construct($attributes);
90
91
        $this->setRules([
92
            'slug' => 'required|alpha_dash|max:150',
93
            'name' => 'required|string|strip_tags|max:150',
94
            'description' => 'nullable|string|max:32768',
95
            'is_public' => 'sometimes|boolean',
96
            'starts_at' => 'required|date',
97
            'ends_at' => 'required|date',
98
            'timezone' => 'nullable|string|max:64|timezone',
99
            'location' => 'nullable|string|strip_tags|max:1500',
100
        ]);
101
    }
102
103
    /**
104
     * Get the public resources.
105
     *
106
     * @param \Illuminate\Database\Eloquent\Builder $builder
107
     *
108
     * @return \Illuminate\Database\Eloquent\Builder
109
     */
110
    public function scopePublic(Builder $builder): Builder
111
    {
112
        return $builder->where('is_public', true);
113
    }
114
115
    /**
116
     * Get the private resources.
117
     *
118
     * @param \Illuminate\Database\Eloquent\Builder $builder
119
     *
120
     * @return \Illuminate\Database\Eloquent\Builder
121
     */
122
    public function scopePrivate(Builder $builder): Builder
123
    {
124
        return $builder->where('is_public', false);
125
    }
126
127
    /**
128
     * Get the options for generating the slug.
129
     *
130
     * @return \Spatie\Sluggable\SlugOptions
131
     */
132
    public function getSlugOptions(): SlugOptions
133
    {
134
        return SlugOptions::create()
135
                          ->doNotGenerateSlugsOnUpdate()
136
                          ->generateSlugsFrom('name')
137
                          ->saveSlugsTo('slug');
138
    }
139
140
    /**
141
     * Activate the resource.
142
     *
143
     * @return $this
144
     */
145
    public function makePublic()
146
    {
147
        $this->update(['is_public' => true]);
148
149
        return $this;
150
    }
151
152
    /**
153
     * Deactivate the resource.
154
     *
155
     * @return $this
156
     */
157
    public function makePrivate()
158
    {
159
        $this->update(['is_public' => false]);
160
161
        return $this;
162
    }
163
}
164